0

I am pretty much copying the code from this thread (Python - copying specific files from a list into a new folder), but can't get it to work and can't see what is wrong. Any insight?

csv file has image name (i.e. image.png) in first column, and significant/insignificant in the next column, but that is not used yet. Just testing it on 10 files right now. The 10 files are in the folder I want to copy from.

    # ----------------------------------------IMPORT PACKAGES -------------------
    import os
    import shutil
    import csv

    # ------------------------------------copy IMAGES using  --------------
    # ----------------------GET PATHS----------------------------------------
    folderpath = os.getcwd() # /home/ubuntu/Deep-Learning/FinalProject/data_random
    destination = '/home/ubuntu/Deep-Learning/FinalProject/data_subset'

    # ------------------LIST OF IMAGE NAMES----------------------------------
    filestofind = []

    with open("labels_test.csv", "r") as f:
        filestofind = [x[0] for x in csv.reader(f) if x]

    print(filestofind)
    # successfully gets list of image names
    # [' image1.png', ' image2.png', ...'image10.png]

    # ------FIND IMAGE IN FOLDER AND COPY AND MOVE TO DESTINATION FOLDER----
    for filename in filestofind:
        print('filename1',filename) #filename1  image1.png - looks ok
        for file in folderpath(filename):
            print('filename2',filename)  #It is seeing this as a string and 
                                         #iterating through the string 
                                        # says it is not callable
                                        # filename2 /
                                        # filename2 h
                                        # filename2 o
                                        # filename2 m        
                   # expected to look for filename1 above in the folderpath
            if os.path.isfile(filename):
                 shutil.copy(filename, destination)
        else:
            print('file does not exist: filename')

    print('All done!')
Jennifer Crosby
  • 185
  • 1
  • 1
  • 14
  • are you getting the right path in ``name`` ? can you check it by ``print(name)`` – PySaad Apr 25 '20 at 09:29
  • if I print filename after the 'for filename in folderpath' line it is taking each character in the string folderpath ( '/home/ubuntu/DL/FP/data_random') and iterating through each character in the for loop, so of course it can't find filename in filepath, it is looking for a letter or symbol, not a filename – Jennifer Crosby Apr 25 '20 at 14:38
  • made some changes to code to show what it is getting at each point. see above. – Jennifer Crosby Apr 25 '20 at 15:27
  • I guess my problem right now is how to get folderpath to be viewed as the folderpath it is looking into and iterating through looking for the filename that is found in the first for loop and not a string. I have tried adding () at the end of folderpath, but it gets the error. I have tried putting os.path before folderpath, but it didn't like that either. – Jennifer Crosby Apr 25 '20 at 15:39
  • Check my answer, I hope it helps – PySaad Apr 25 '20 at 15:49

1 Answers1

0

Below code might help to resolve the issue you are facing-


    all_files = [f for f in os.listdir(folderpath) if os.path.isfile(os.path.join(folderpath, f))]
    # This returns all the files you have in your search directory
    files_to_copy = [x for x in filestofind if x in all_files]
    # This returns the common files you want to copy
    for file_to_copy in files_to_copy:
        shutil.copy(file_to_copy, destination)

PS: You can copy the above after # ------FIND IMAGE IN FOLDER AND COPY AND MOVE TO DESTINATION FOLDER----"

Reference:

https://docs.python.org/3/library/os.html#os.walk

PySaad
  • 1,052
  • 16
  • 26
  • That worked beautifully. Thanks!! I'm slowly getting better at these for loops to manipulate the data, but it stumps me more often then not. Just keep trying I guess. Thanks again!! – Jennifer Crosby Apr 25 '20 at 23:02