Skat1337
Skat1337

Reputation: 35

Moving files with shutil.move

i want to move files from a folder to antother. I found out there is a function in the shutil module called

shutil.move(src,dest)

But i cant get it to work it always says that the files dont exist. Heres my Code:

source = 'C:\\Users\\User\\Desktop\\Test1'
dest1 = 'C:\\Users\\User\\Desktop\\Test2'

files = os.listdir(source)

for f in files:
    if (f.startswith("Test")):
        shutil.move(f, dest1)

The folders and files all exist.

Error:

IOError: [Errno 2] No such file or directory: 'Test1.csv'

Anyone knows how to fix?

Upvotes: 1

Views: 2355

Answers (1)

tzaman
tzaman

Reputation: 47850

listdir will just give you the filenames inside the directory, not the fully qualified names.
You can join them back together:

for f in files:
    if f.startswith("Test"):
        shutil.move(os.path.join(source, f), dest1)

Upvotes: 3

Related Questions