gpt4 book ai didi

Python - 将特定文件从子文件夹复制到目标,从文本文件获取文件名

转载 作者:行者123 更新时间:2023-12-04 01:30:15 25 4
gpt4 key购买 nike

我想让我的脚本从列表 (txt) 中读取名称列表,然后在具有子文件夹的选定文件夹中搜索这些名称,然后将这些文件复制并粘贴到另一个选定的文件夹。我的脚本运行没有错误但没有结果。

我的脚本:

import os
import os.path
import shutil

textFile = ("D:\\Test\\list.txt")
sourceFolder = ("D:\\Test")
destinationFolder = ("D:\\")

filesToFind = []
with open(textFile, "r") as tx:
for row in tx:
filesToFind.append(row.strip())

for root, dirs, filename in os.walk(sourceFolder):
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)

最佳答案

还没有测试过,但我认为这会起作用 - 改变这个:

for root, dirs, filename in os.walk(sourceFolder):
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)

对此:

for root, dirs, filenames in os.walk(sourceFolder):
for filename in filenames:
if filename in filesToFind:
f = os.path.join(root, filename)
shutil.copy(f, destinationFolder)

关于Python - 将特定文件从子文件夹复制到目标,从文本文件获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61168619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com