Python - copying specific files from a list into a new folder

你。 提交于 2019-12-11 08:13:11

问题


I am trying to get my program to read a list of names from a file (say .txt), then search for those in a selected folder and copy and paste those files to another selected folder. My program runs without errors but does not do anything:

Code - updated:

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)

Update - runs without errors but does not move any files.


回答1:


The last part of your program might work better this way:

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

Otherwise it's pretty much unknown where you copy your files from, current folder, maybe, and why did you need to input folderPath earlier, if you don't use it.

btw, file is a reserved word in python, I'd recommend to use another name for your variable, that does not coincide with python reserved words.




回答2:


Your last section has a problem.

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

The first for loops over each filename in the directory, which is perfectly understandable.

The second for is an error, because files does not exist. What did you intend it to do?




回答3:


Code that works -

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")

Note - Need to included the file extension in the file being read. Thanks to @lenik and @John Gordon for the help getting there! Time to refine it to make it more userfriendly



来源:https://stackoverflow.com/questions/51528103/python-copying-specific-files-from-a-list-into-a-new-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!