I am comparing filenames in a directory with a list of filenames I generated from an excel specific sheet then moving the files with matching filenames to a new folder called 'moved_files'.Why is my if statement ignored and no files are moved when the code runs and prints "done" at the end?
I have used print statements before the last if statement to see the pdb_filename and filename_gen and some of the filename_gen do match with pdb_filename. The print statement doesn't print although there are files with .pdb at the end in the directory.
Here is the code
for filename_gen in list_filename_gen:
for pdb_filename in os.listdir(directory):
if pdb_filename.endswith(".pdb"):
print(pdb_filename)
if print filename_gen ==print pdb_filename :
shutil.move(os.path.join(directory, pdb_filename),'/Users/fififoufa/Desktop/files_moved/%s' % (pdb_filename))
print("done")
I expect to see files moved when the names exactly match e.g files\xTMEM16A_dimer_OPM_PI4P\500_4.pdb and files\xTMEM16A_dimer_OPM_PI4P\500_4.pdb
printreturnsNone, so your comparison of twoprintcalls will always returnTrue. Nowhere have you compared the file names. If you suspect that a particularifis ignored, where is your trace of the values just before they're compared? See this lovely debug blog for help. This posting has a lot of superfluous code, and hasn't displayed the values critical to the problem. Please update.list_clusternumberis empty becausews.nrows <= 5orws.ncols == 0. This would mean thatlist_filename_genis also empty. Another possibility is thatdirectorydoes not contain any files.if print(filename_gen)==print(pdb_filename):Not exactly sure what this line is meant to be doing, but it's going to compare the two print functions, not the values they're printing. This will always become ifNone == None:which evaluates asTrue. If you were trying to see if the two names are equal, tryif filename_gen == pdb_filename:instead.