Lakshmi Narayanan
Lakshmi Narayanan

Reputation: 5362

copy files from folder to folder python

I know this question has been answered, but my application uses the solutions, yet am facing bugs, that am not able to solve.

I have a list of numbers in a text file, that denote the image numbers that should be copied. The list is similar to this

    7348
    7352
    7357
    7360
    7380
    7381
    .
    .
    .

The images are with name

    IMG_7348.jpg
    IMG_7349.jpg
    .
    .
    .

Using the numbers from the text file, I want to copy only those images to a different folder. This is the python code I wrote for the same

    import os
    import shutil
    src = input('Enter the source folder');
    dest = input('Enter the destination folder');
    src_files = os.listdir(src)
    with open("image_numbers.txt") as f:
        lines = [line.rstrip('\n') for line in open('image_numbers.txt')]
        for line in lines:  
            numbers_str = line
            #print(numbers_str)
            temp2 = str('IMG_')+numbers_str+str('.jpg')
            #print(temp2)
            for name_im in src_files:
                #print(name_im)
                print(name_im == temp2)
                if name_im == temp2:
                    src_files_filt = temp2
                    #print('stored')
            #numbers_float = [float(x) for x in numbers_str]
            #map(float,numbers_str) works too
    for file_name in src_files_filt:
        full_file_name = os.path.join(src, file_name)
        if (os.path.isfile(full_file_name)):
            shutil.copy(full_file_name, dest)
  1. When I use the print statements, I get to see that the reformed image name and the name from the src are the same, yet the statement

    print(name_im == temp2)
    

gives me

    false

I am not able to figure out the reason

Can you please help me fix the error?

Upvotes: 1

Views: 221

Answers (3)

ejg
ejg

Reputation: 196

You are already opening "image_numbers.txt"and iterating through each line, so you don't need to open it again to iterate through to strip "\n".

lines = [line.rstrip('\n') for line in open('image_numbers.txt')]

This can be achieved more easily by striping "\n" when iterating for line in f. When I ran your code, it did not remove the "\n" which prevented the it from evaluating True in print(name_im == temp2). Additionally, you can't iteratefor file_name in src_files_filt:, because src_files_filt in your code is not a list, rather it is the name of a single file.

Try the following:

import os
import shutil
src = input('Enter the source folder');
dest = input('Enter the destination folder');
src_files = os.listdir(src)
src_files_filt = []
with open("image_numbers.txt") as f:
    for line in f:
        numbers_str = line.rstrip()
        #temp2 = "IMG_%s.jpg" %(numbers_str) #another str manipulation method
        temp2 = str('IMG_')+numbers_str+str('.jpg') 
        #print(temp2)
        for name_im in src_files:
            print(name_im)
            print(name_im == temp2)
            if name_im == temp2:
                src_files_filt.append(temp2)

for file_name in src_files_filt:
    #print(file_name)
    full_file_name = os.path.join(src, file_name)
    #print(full_file_name)
    if (os.path.isfile(full_file_name)):
         shutil.copy(full_file_name, dest)enter code here

Upvotes: 1

qwertyuip9
qwertyuip9

Reputation: 1632

I'm not too sure why the 2 errors were occurring. The second problem you detailed didn't occur for me but I fixed your code to make it cleaner and more pythonic. There would have been a problem with "src_files_filt" since the last for loop iterated it like a list but "src_files_filt" was only a string. I made it such that the script performs the file change right after the filenames are matched.

Edit: Looking over your program again for problem 1, some of the values should return false since there are other files present that are not in the text file. If you place the print statement inside the if block, it should always return true as expected.

import os
import shutil

src = input('Enter the source folder');
dest = input('Enter the destination folder');
src_files = os.listdir(src)

with open("image_numbers.txt") as f:
  lines = [line.rstrip('\n') for line in open('image_numbers.txt')]
  for line in lines:  
      numbers_str = line

      temp1 = 'IMG_' + numbers_str + '.jpg'

      for name_im in src_files:
          if name_im == temp1:
              full_file_name = os.path.join(src, temp1)
              if (os.path.isfile(full_file_name)):
                  shutil.copy(full_file_name, dest)

Upvotes: 1

Irfan
Irfan

Reputation: 5130

print(str(name_im) == temp2) return true

Upvotes: 1

Related Questions