没有错误,但os.copy不会复制文件

时间:2013-10-27 12:02:20

标签: python

我知道这是非常基本的,但是当显然没有错误时我不知道如何修复它。感谢

import os
import shutil

# First, create a list and populate it with the files
# you want to find (1 file per row in myfiles.txt)
files_to_find = []
with open('C:/Users/me/Desktop/test/_FileList.txt') as fh:
    for row in fh:
        files_to_find.append(row.strip)

# Then we recursively traverse through each folder
# and match each file against our list of files to find.
for root, dirs, files in os.walk('C:/Users/me/Desktop/test/newfolder'):
    for _file in files:
        if _file in files_to_find:
            # If we find it, notify us about it and copy it it to C:\NewPath\
            shutil.copy(os.path.abspath(root + '/' + _file), 'C:/Users/me/Desktop/test/')

1 个答案:

答案 0 :(得分:3)

缺少对.strip()方法的调用(); files_to_find包含方法引用,而不是文件名字符串列表。

>>> row = 'abc\n'
>>> row.strip
<built-in method strip of str object at 0x0000000002C5EAF8>
>>> row.strip()
'abc'

替换以下行:

files_to_find.append(row.strip)

使用:

files_to_find.append(row.strip())