6

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.

JasonDL
  • 127
  • 1
  • 2
  • 12

3 Answers3

3

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.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Thanks! I will give that a shot when I get to work in the morning. I was using `folderPath` earlier to get the folder I want the program to loop through and look for the files in. Looking back at it now I see your point that I was not calling it anywhere. Thanks for the file heads up I will change that as well. – JasonDL Jul 26 '18 at 00:55
  • Also realized I threw the label in the wrong spot. The top code set is what was the updated code. Originally, I was not calling it but in the updated code I was with `for file in os.listdir(folderPath)`. Thoughts on why that might not work? – JasonDL Jul 26 '18 at 01:00
  • 1
    @JasonLeach `os.listdir()` gives you just file names (without path). you have to supply path to be able to copy or open or do anything with the files. hence, `os.path.join( path, name)` – lenik Jul 26 '18 at 02:29
  • I tested it this morning and with no result. I added `print(name)` after the `name = ` statement but it tells me name is not defined. thought on how I could print that out so I can attempt to debug? – JasonDL Jul 26 '18 at 14:56
  • @JasonLeach please, show the code, I don't believe `name` could be empty right after the assignment. – lenik Jul 26 '18 at 15:19
  • @JasonLeach `print ("file does not exist" & name)` -- you need a comma `,` instead of ampersand `&` here – lenik Jul 26 '18 at 15:20
  • Code updated - I tried it in two places (see above) and in both cases it says it is not defined. Is it possible that the `os.path.join` needs a `\` between `folderPath, target' ? Sorry new to `os.` and python in general. – JasonDL Jul 26 '18 at 15:50
  • @JasonLeach `print name` statement should have the same indentation as the previous `name =` line, otherwise it won't work. indentation is quite important in python. – lenik Jul 26 '18 at 15:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176815/discussion-between-jason-leach-and-lenik). – JasonDL Jul 26 '18 at 16:04
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?

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I think I thought I was trying to say: "for each `file` (renamed `target`) name in `filesToFind` list from `filePath`, look in `folderPath` folder for the file names and copy and paste them to the `destination` folder." In regards to `files` I wanted to look at all the `file` names in `filePath` and loop through the folder. If that makes sense? I removed it and updated the code above - question assuming the `for file in` loop is correct, should it be `for file in filePath` or `for file in folderPath`? – JasonDL Jul 26 '18 at 14:28
2

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

Bobobot
  • 65
  • 1
  • 8
JasonDL
  • 127
  • 1
  • 2
  • 12