#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Code will not write to file till IDLE Closed &Reopened


    I am writing a program that builds a couple of lists that are then supposed to be written out to files for storage.

    HOWEVER, when the program executes, the file is created in the working directory but NOT populated with the items in the list. Only after I close the Python Shell (IDLE) and then re-open it, are the items in the list written to the file.

    If I try to delete the file before I close and re-open IDLE, I get an error message saying file is in use.

    I don't know why the program seems to be getting stuck.
    I have used the same block of code earlier in the program to open a file and write to it, and it worked fine.

    See code block below (the one that causes the issue, not the one that works)

    #This section will write the payments out list to the payments out file
    my_file03 = open("pmtsout.txt", 'w')
    for line in pmtsoutl:
    print >> my_file03, line
    my_file03.close
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,902
    Rep Power
    481
    Code:
    #This section will write the payments out list to the payments out file
    my_file03 = open("pmtsout.txt", 'w')
    for line in pmtsoutl:
        print >> my_file03, line
    my_file03.close()################ method call close needs ()
    # the problem probably occurs elsewhere in code you didn't show.
    
    
    
    # the modern alternative, use the context
    with open("pmtsout.txt", 'w') as my_file03:
        for line in pmtsoutl:
            print >> my_file03, line

    Comments on this post

    • Nyktos agrees
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo