简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM