简体   繁体   English

使用shutil.move移动文件

[英]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模块中有一个函数叫做

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' IOError:[Errno 2]没有这样的文件或目录:'Test1.csv'

Anyone knows how to fix? 有人知道如何解决吗?

listdir will just give you the filenames inside the directory, not the fully qualified names. listdir只会为您提供目录中的文件名,而不是全限定名。
You can join them back together: 您可以将他们重新加入在一起:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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