【发布时间】:2019-07-25 12:37:12
【问题描述】:
我想根据.txt 文件中的匹配文件名将文件从一个大文件夹复制到另一个文件夹。
我的list.txt 文件包含文件名:
S001
S002
S003
另一个大文件夹包含许多 ex 文件。 S001, S002, S003, S004, S005.
我只想从这个大文件夹中复制与我的list.txt 文件中的文件名匹配的文件。
我尝试过 Bash、Python - 不工作。
for /f %%f in list.txt do robocopy SourceFolder/ DestinationFolder/ %%f
也不工作。
我在 Python 中的逻辑不起作用:
import os
import shutil
def main():
destination = "DestinationFolder/copy"
source = "SourceFolder/MyBigData"
with open(source, "r") as lines:
filenames_to_copy = set(line.rstrip() for line in lines)
for filenames in os.walk(destination):
for filename in filenames:
if filename in filenames_to_copy:
shutil.copy(source, destination)
在 Bash、Python 或 R 中有任何答案吗?
谢谢
【问题讨论】:
-
您能具体解释一下 Python 尝试的哪一部分不起作用吗?
-
你想复制到哪里?在另一个文件夹中?
-
是的,我想将匹配的文件名复制到另一个文件夹中。