python - Python - 将特定文件从子文件夹复制到目标,从文本文件中获取文件名
问题描述
我想让我的脚本从列表(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)
推荐阅读
- android-studio - 缺少 Android Studio JDK 位置
- reactjs - 反应警报未以正确的样式显示
- c# - 如何在运行时请求管理员权限?
- angular - Service Worker 不缓存 manifest.webmanifest
- oracle - 如何解决问题?导出为 PDF 文件时 Crystal Report 2016 上的字符
- javascript - 如何在我的 asp.net 应用程序中添加从 GitHub 下载的 JavaScript 控件(JavaScript 包)
- tfs-2018 - TFS 2018 - 集合级别权限
- java - Jena Reasoner 无法推断
- performance - GPU 代码的微结构优化
- node.js - Node.Js - Socket.io - Angular -> 如何通过多个事件优先接收数据/发送数据?