Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 若子目录中的文件在列表中,则复制到目标_Python - Fatal编程技术网

Python 若子目录中的文件在列表中,则复制到目标

Python 若子目录中的文件在列表中,则复制到目标,python,Python,我是初学者。我有一个.txt文件,其中包含感兴趣文件的文件名。我想遍历一个目录和子目录,如果一个文件与列表中的一个文件名匹配,那么它将被复制到一个目标 files_to_find = [] with open('outliers.txt', 'r') as f: files_to_find = [line.strip() for line in f] for subdir, dirs, files in os.walk(rootdir): for file in fil

我是初学者。我有一个.txt文件,其中包含感兴趣文件的文件名。我想遍历一个目录和子目录,如果一个文件与列表中的一个文件名匹配,那么它将被复制到一个目标

files_to_find = []
with open('outliers.txt', 'r') as f:
    files_to_find = [line.strip() for line in f]

for subdir, dirs, files in os.walk(rootdir):
        for file in files:
            if file in files_to_find:
                fileSrc = os.path.join(os.path.abspath(subdir), file)
                shutil.copyfile(fileSrc, os.path.join('C:/blah/blah/dest', file))

但这无法将文件名与列表中的元素(它们就在那里)匹配—我缺少什么?谢谢。

看起来不错。您是否可以打印
文件
文件以查找
,以查看是否有重大错误(绝对路径、大小写问题)。可能存在大小写问题。调试的步骤1:
打印(要查找的文件)
。调试完成。是的,我可以打印要查找的文件,看起来很好。下一步是什么/请尝试
打印(文件,要查找的文件)
检查您要比较的内容您的代码没有问题,因此问题一定是文件的内容。我想这可能是一个大小写问题,或者您忘记在文件名中包含文件扩展名。