0

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

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/')
4

1 回答 1

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())
于 2013-10-27T12:04:56.077 回答