Python - 将列表中的特定文件复制到新文件夹中
Python - copying specific files from a list into a new folder
我正在尝试让我的程序从文件(例如 .txt)中读取姓名列表,然后在选定的文件夹中搜索这些文件并将这些文件复制并粘贴到另一个选定的文件夹。我的程序运行没有错误,但没有执行任何操作:
代码 - 已更新:
import os, shutil
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
filePath = filedialog.askopenfilename()
folderPath = filedialog.askdirectory()
destination = filedialog.askdirectory()
filesToFind = []
with open(filePath, "r") as fh:
for row in fh:
filesToFind.append(row.strip())
#Added the print statements below to check that things were feeding correctly
print(filesToFind)
print(folderPath)
print(destination)
#The issue seems to be with the copy loop below:
for target in folderPath:
if target in filesToFind:
name = os.path.join(folderPath,target)
print(name)
if os.path.isfile(name):
shutil.copy(name, destination)
else:
print ("file does not exist", name)
print(name)
更新 - 运行无误但不移动任何文件。
您的程序的最后一部分可能会以这种方式运行得更好:
for file in files:
if file in filesToFind:
name = os.path.join( folderPath, file )
if os.path.isfile( name ) :
shutil.copy( name, destination)
else :
print 'file does not exist', name
否则几乎不知道您从哪里复制文件,也许是当前文件夹,如果您不使用它,为什么需要提前输入 folderPath
。
顺便说一句,file
是 python 中的保留字,我建议您为您的变量使用另一个名称,该名称与 python 保留字不一致。
你的最后一节有问题。
for file in os.listdir(folderPath):
for file in files:
if file in filesToFind:
shutil.copy(file, destination)
第一个 for
遍历目录中的每个文件名,这是完全可以理解的。
第二个for
是一个错误,因为files
不存在。你打算用它做什么?
有效的代码 -
import os
import shutil
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
filePath = filedialog.askopenfilename()
folderPath = filedialog.askdirectory()
destination = filedialog.askdirectory()
# First, create a list and populate it with the files
# you want to find (1 file per row in myfiles.txt)
filesToFind = []
with open(filePath, "r") as fh:
for row in fh:
filesToFind.append(row.strip())
# Had an issue here but needed to define and then reference the filename variable itself
for filename in os.listdir(folderPath):
if filename in filesToFind:
filename = os.path.join(folderPath, filename)
shutil.copy(filename, destination)
else:
print("file does not exist: filename")
注意 - 需要在正在读取的文件中包含文件扩展名。感谢@lenik 和@John Gordon 的帮助!是时候对其进行改进,使其更加用户友好
我正在尝试让我的程序从文件(例如 .txt)中读取姓名列表,然后在选定的文件夹中搜索这些文件并将这些文件复制并粘贴到另一个选定的文件夹。我的程序运行没有错误,但没有执行任何操作:
代码 - 已更新:
import os, shutil
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
filePath = filedialog.askopenfilename()
folderPath = filedialog.askdirectory()
destination = filedialog.askdirectory()
filesToFind = []
with open(filePath, "r") as fh:
for row in fh:
filesToFind.append(row.strip())
#Added the print statements below to check that things were feeding correctly
print(filesToFind)
print(folderPath)
print(destination)
#The issue seems to be with the copy loop below:
for target in folderPath:
if target in filesToFind:
name = os.path.join(folderPath,target)
print(name)
if os.path.isfile(name):
shutil.copy(name, destination)
else:
print ("file does not exist", name)
print(name)
更新 - 运行无误但不移动任何文件。
您的程序的最后一部分可能会以这种方式运行得更好:
for file in files:
if file in filesToFind:
name = os.path.join( folderPath, file )
if os.path.isfile( name ) :
shutil.copy( name, destination)
else :
print 'file does not exist', name
否则几乎不知道您从哪里复制文件,也许是当前文件夹,如果您不使用它,为什么需要提前输入 folderPath
。
顺便说一句,file
是 python 中的保留字,我建议您为您的变量使用另一个名称,该名称与 python 保留字不一致。
你的最后一节有问题。
for file in os.listdir(folderPath):
for file in files:
if file in filesToFind:
shutil.copy(file, destination)
第一个 for
遍历目录中的每个文件名,这是完全可以理解的。
第二个for
是一个错误,因为files
不存在。你打算用它做什么?
有效的代码 -
import os
import shutil
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
filePath = filedialog.askopenfilename()
folderPath = filedialog.askdirectory()
destination = filedialog.askdirectory()
# First, create a list and populate it with the files
# you want to find (1 file per row in myfiles.txt)
filesToFind = []
with open(filePath, "r") as fh:
for row in fh:
filesToFind.append(row.strip())
# Had an issue here but needed to define and then reference the filename variable itself
for filename in os.listdir(folderPath):
if filename in filesToFind:
filename = os.path.join(folderPath, filename)
shutil.copy(filename, destination)
else:
print("file does not exist: filename")
注意 - 需要在正在读取的文件中包含文件扩展名。感谢@lenik 和@John Gordon 的帮助!是时候对其进行改进,使其更加用户友好