0

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

4
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Commented Oct 21, 2019 at 16:06
  • First of all, print returns None, so your comparison of two print calls will always return True. Nowhere have you compared the file names. If you suspect that a particular if is 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. Commented Oct 21, 2019 at 16:08
  • Maybe list_clusternumber is empty because ws.nrows <= 5 or ws.ncols == 0. This would mean that list_filename_gen is also empty. Another possibility is that directory does not contain any files. Commented Oct 21, 2019 at 16:27
  • 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 if None == None: which evaluates as True. If you were trying to see if the two names are equal, try if filename_gen == pdb_filename: instead. Commented Oct 21, 2019 at 16:37

1 Answer 1

1

You're not exactly comparing the two file names, you're instead comparing two print calls which would return True. if filename_gen == pdb_filename:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.