【问题标题】:Python - copying specific files from a list into a new folderPython - 将列表中的特定文件复制到新文件夹中
【发布时间】:2018-07-25 21:42:30
【问题描述】:

我正在尝试让我的程序从文件(例如 .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)

更新 - 运行没有错误,但不移动任何文件。

【问题讨论】:

  • 你想要row.strip() 而不是row.strip。注意括号。
  • for files in folderPath: 没有按照你的想法做。你想要for file in os.listdir(folderPath):
  • 我将其更改为 filesToFind.append(rowstrip())for file in os.listdir(folderPath): if file in filesToFind: 但得到相同的结果(无结果)
  • 更新问题以包含您最近的代码。
  • 已更新。抱歉仍在学习 Stack Overflow 的最佳实践

标签: python python-3.x tkinter copy-paste shutil


【解决方案1】:

您的程序的最后一部分可能会以这种方式更好地工作:

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 保留字不一致。

【讨论】:

  • 谢谢!我早上上班时会试一试。我之前使用folderPath 来获取我希望程序循环并在其中查找文件的文件夹。现在回头看它,我明白你的观点,我没有在任何地方调用它。感谢您提供文件提示,我也会更改它。
  • 还意识到我把标签放在了错误的位置。顶部代码集是更新的代码。最初,我没有调用它,但在更新的代码中我使用了for file in os.listdir(folderPath)。关于为什么这可能行不通的想法?
  • @JasonLeach os.listdir() 只给你文件名(没有路径)。您必须提供路径才能复制或打开文件或对文件执行任何操作。因此,os.path.join( path, name)
  • 我今天早上测试了它,但没有结果。我在name = 语句之后添加了print(name),但它告诉我名称未定义。考虑过如何将其打印出来以便尝试调试?
  • @JasonLeach 请出示代码,我不相信name 在分配后会立即为空。
【解决方案2】:

你的最后一节有问题。

for file in os.listdir(folderPath): 
    for file in files:
        if file in filesToFind:
            shutil.copy(file, destination)

第一个for 循环遍历目录中的每个文件名,这是完全可以理解的。

第二个for 是错误的,因为files 不存在。你打算做什么?

【讨论】:

  • 我想我想我想说的是:“对于来自filePathfilesToFind 列表中的每个file(重命名为target)名称,请在folderPath 文件夹中查找文件名称并将它们复制并粘贴到 destination 文件夹中。”关于files,我想查看filePath 中的所有file 名称并遍历文件夹。如果这有意义吗?我删除了它并更新了上面的代码 - 假设for file in 循环是正确的,应该是for file in filePath 还是for file in folderPath
【解决方案3】:

有效的代码 -

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 的帮助!是时候对其进行改进以使其更加用户友好了

【讨论】:

  • 最后一行应该是:print(filename, "未复制")
猜你喜欢
  • 2021-05-13
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2018-05-17
  • 1970-01-01
  • 2014-12-03
  • 2023-03-29
相关资源
最近更新 更多