lundi 21 octobre 2019

why is the if statement ignored python

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 after the if statement doesn't print.

Here is the code

excel_filename = input("Enter excel filename: ")
wb = xlrd.open_workbook('/Users/fififoufa/Desktop/%s.dlg.xls' % (excel_filename))
ws = wb.sheet_by_name("Dock List")

pdbfile_pattern = r'files''\\' + excel_filename

list_clusternumber = []
list_filename_gen = []

for row in range(5, ws.nrows):
    for col in range(0, ws.ncols):
        list_clusternumber.append(ws.cell(row, col).value)

for num in list_clusternumber:
    pdb_file_gen_1 = pdbfile_pattern + "\\" + str(num) + "_0" + ".pdb"
    pdb_file_gen_2 = pdbfile_pattern + "\\" + str(num) + "_1" + ".pdb"
    pdb_file_gen_3 = pdbfile_pattern + "\\" + str(num) + "_2" + '.pdb'
    pdb_file_gen_4 = pdbfile_pattern + "\\" + str(num) + "_3" + ".pdb"
    pdb_file_gen_5 = pdbfile_pattern + "\\" + str(num) + "_4" + ".pdb"
    list_filename_gen.append(pdb_file_gen_1)
    list_filename_gen.append(pdb_file_gen_2)
    list_filename_gen.append(pdb_file_gen_3)
    list_filename_gen.append(pdb_file_gen_4)
    list_filename_gen.append(pdb_file_gen_5)

for filename_gen in list_filename_gen:
    for pdb_filename in os.listdir(directory):
        if pdb_filename.endswith(".pdb"):
            if print(filename_gen)==print(pdb_filename):
                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

Aucun commentaire:

Enregistrer un commentaire