1

I'm trying to get this nested loop to work for a bigger program. This is just a small portion of my code

I want my program to match a file name in a folder to a file name in a text document, and if they match do something. I have no clue why the following nested loop is not working.

allGood = open("allGood.txt", "r")
folder = "C:\your\folder\path\here"

for item in allGood:
    for file in os.listdir(folder):

        if file == item:
            print "in item loop" + item
            print "Do a thing"
    print "1 loop completed"

The contents of "allGood.txt"

document10080.pdf
document10098.pdf
document10119.pdf
document10172.pdf
document10178.pdf
and so on

The problem is when it gets to the IF statement. It should match only once per loop, but it doesn't. I am only getting a huge output of "1 loop completed"

Output from inserting print(file, item)

('document10486.pdf', 'document10080.pdf\n')
('document10487.pdf', 'document10080.pdf\n')
('document10488.pdf', 'document10080.pdf\n')
('document10489.pdf', 'document10080.pdf\n')
('document1049.pdf', 'document10080.pdf\n')

I see my problem now

15
  • Please read and follow the posting guidelines in the help documentation. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Commented Nov 8, 2017 at 22:09
  • @Prune so do you want me to add a "folder" object? what else am I missing? Commented Nov 8, 2017 at 22:11
  • What is folder in your second for? It isn't defined anywhere Commented Nov 8, 2017 at 22:11
  • @SPYBUG96: As I said, the posting should be self-contained. The input file is missing and the return of os.listdir won't match your needs. Hard-code those lists, and you should be fine. Commented Nov 8, 2017 at 22:13
  • 1
    @SPYBUG96 Welcome to the homo sapiens club, which I generally refer to as H. Sap. Been there, done that, could make a football-field-sized flag from the T-shirts. Commented Nov 8, 2017 at 22:21

2 Answers 2

3

I don't have enough reputation to comment, so i'll try an answer with insufficient informations. Basically you never have a match. That's why you never enter the second loop.

Did you consider handling the filenames as sets?:

real_files = set(os.listdir(folder))
good_files = set(open("allGood.txt", "r").readlines())

matching_files = good_files.intersection(real_files)
for file in matching_files:
    pass # do something

Also the text-file entries may contain white-spaces; consider using 'strip`, e.g:

import string
...
good_files = set(map(string.strip, open("allGood.txt", "r").readlines()))
...

or somewhat less "Perl-ish" ;-)

...
good_files_raw = open("allGood.txt", "r").readlines()
good_files = set(map(string.strip, good_files_raw))
...
Sign up to request clarification or add additional context in comments.

2 Comments

I was just about to suggest this tactic -- good work. BTW, lack of reputation is not an excuse to post a comment as an answer. This qualifies as an answer, however.
Yeah I've had a long day and I just completely forgot about that Thanks
2

Remember, allGood is a file handle, not the file contents. Perhaps what you need is something like this:

folder = os.listdir("C:\your\folder\path\here")

with open("allGood.txt", "r") as allGood:
    for line in allGood:
        line = line.strip()
        if line in folder:
        ...

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.