Python 复制和粘贴

Python Copy and Past

我正在尝试使用 Python Shutil 模块将文件从一个文件夹复制粘贴到另一个文件夹,但出现错误,我不确定是什么问题。

import os
import shutil

source = os.listdir("D:\Personal\TEST\SRC")
print source
destination = "D:\Personal\TEST\DEST"

for files in source:
    if files.endswith('.txt'):
        shutil.copy(files,destination)

Error:
File "C:/Users/xxx/xxx/config/scratches/test.py", line 10, 
in <module>
shutil.copy(files,destination)
File "C:\Python27\Lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\Lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'TEST.txt'

非常感谢任何帮助 谢谢

试试这个:

import os
import shutil

source = r"D:\Personal\TEST\SRC"
destination = r"D:\Personal\TEST\DEST"

for file in [os.path.join(source, x) for x in os.listdir(source)]:
    if file.endswith('.txt'):
        shutil.copy(file, os.path.join(destination, os.path.basename(file)))