1

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?

1
  • I edited my explanation to explain for problem 1 which I think was just due to the placement of the print statement. Commented Sep 12, 2015 at 15:37

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

ok, it is not working again, for some reason. The same code, different folders and files, but it is not working now. any debugging idea ?
Is the script running to completion or is there an exception thrown? A few things come to mind to check: (1) Does printing src_files show the correct files in the source folder? (2) Making sure the image_numbers.txt is in the same directory as your python file. (3) Try printing the "line" variable in the for loop to see if that's what you expect it to be.
I ran the basics mate. the error is the strings don't match. @qwertyuip9
there's no error thrown. the copy operation doesn't happen cuz no strings match
1

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.