kitchen800
kitchen800

Reputation: 227

Moving first file in folder to another folder

I am trying to move the first file in a folder into another folder using python. I am using shutil in order to do this.

I know that the following will move the whole S folder into the D folder. but how do I choose only the first file within the folder?

S = '/Users/kitchensink/Desktop/Sfolder'
D = '/Users/kitchensink/Desktop/Dfolder'
shutil.move(S, D)
print("moved")

Upvotes: 0

Views: 254

Answers (1)

Erik
Erik

Reputation: 867

You can use glob to find the files in a folder. For example if you want to have a list with all files in SFolder you can do this:

import glob
s_files = glob.glob('/Users/kitchensink/Desktop/Sfolder/*')

To get the first file, simply take the first element from s_files. After this you can still use shutil to do the actual moving.

Upvotes: 1

Related Questions