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