0

I have created variables that I need to drop into txt files. I am not having any success. My program will create the header information, but stops writing to the file after that. The top loop is my "Order Header" and the machines loop is my "Order Detail". I've tried debugging, and looking at it from every angle. I can't figure out what I'm doing wrong. The program is writing all of the text files and appending for the XHORNO and XHCSNO. As far as troubleshooting, I have tried print statements (print "header" print "detail", and print "machine" ) to verify that the code was hitting each loop as expected. That effort resulted in the correct output. However it won't write the detail loops variables to file.

for k, v in atlantic_billing.iteritems():
    # print "header"
    #CREATE XHORNO
    XHORNO = str(digits + counter)
    print XHORNO
    with open(XHORNO + ".txt", 'w') as order:
        XHCSNO = k
        machines = v
        #create order header information
        order.write(XHORNO+"\n")
        order.write(XHCSNO+"\n")
        line = 1
        counter = counter + 1
        for machine in machines :
            try:
                XDORNO = XHORNO
                XDORSQ = line
                line = line + 1
                XDITD1 = ranpak_dict[machine]['MODEL']
                XDITD2 = ranpak_dict[machine]['SN']
                XDCAVC = ranpak_dict[machine]['TOTAL']
                order.write(XDORDQ + "\n")
                order.write(XDITD1 + "\n")
                success.append(machine)
                lines = lines + 1

            except :
                issues.append(machine)
                problems = problems + 1
0

1 Answer 1

3

Best guess with the given information... each loop is getting down to:

lines = lines + 1

And then throwing a NameError because lines is undefined (you called it line before). Since your except clause is catching all exceptions, the program quietly adds that entry to problems and moves on.

This is a good example of why you should try to be as specific as possible about what errors you catch in your except clauses.

Edit: ...but I suppose it should still have written the lines to the file at that point? So... possibly still more information needed. But definitely narrow down your except to make it easier to troubleshoot.

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

3 Comments

lines is actually a variable created above the initial loop. However, I removed it just to verify it wasn't causing the issue and that didn't fix it. Thanks for the suggestion on the except clause, though! I will keep that in mind moving forward.
@AlliDeacon Start by eliminating the except block altogether. If the error you get is one you'll need to catch, then add the block back with that single type of error. If you get more errors you need to handle, add them to the clause one by one. Eventually, hopefully you'll get to the actual error that's causing problems.
Thanks so much! I learned something new! I didn't realize that I could use except this way. I found the error, and corrected it. All is working like it should.

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.