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

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Question File not getting closed in Python ('W') mode


    Hi!

    I opened a file for writing

    and i write a pickle object ( dictionary object ,file )

    and close the file

    but the file is not getting closed

    fil1=open('C:\\Python23\\inventory.txt','w')
    print inventory
    pickle.dump(inventory,fil1)
    fil1.close()
    print inventory
    print fil1.mode
    fil1.close()
    print fil1.mode


    Please help me

    Thanz
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Instead of using fil1=open('C:\\Python23\\inventory.txt','w'), just use open('C:\\Python23\\inventory.txt','w'), it will work the same but you wont need to declare it with a variable, also, it will close automatically once it is done.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    @ †Yegg†:
    When I use your suggestion, what exactly is the second argument for pickle?

    @ shaharpan:
    I don't see where you check if the file was closed. fl1.mode just returns the mode in which the file was opened (r,w,a,...). To check if it is closed, use fl1.closed.

    Regards, mawe
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by mawe
    Hi!

    @ †Yegg†:
    When I use your suggestion, what exactly is the second argument for pickle?

    @ shaharpan:
    I don't see where you check if the file was closed. fl1.mode just returns the mode in which the file was opened (r,w,a,...). To check if it is closed, use fl1.closed.

    Regards, mawe
    thank you

    I got some other options.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just out of interest, why was the file "not" being closed causing you problems? Or are we just not seeing that part of the program; I've never heard of an issue where a file wouldn't close.

    For completeness, you can also check see if a file is closed visually by printing the files instance - this is most useful in the Python shell, you should use the closed attribute if you want to use this in a program.

    Code:
    >>> aFile = file('/Users/Mark/Desktop/sample.txt', 'w')
    >>> aFile
    <open file '/Users/Mark/Desktop/sample.txt', mode 'w' at 0x4a4a0>
    >>> aFile.close()
    >>> aFile       
    <closed file '/Users/Mark/Desktop/sample.txt', mode 'w' at 0x4a4a0>
    >>> aFile.closed
    True
    >>>
    This method is also very useful for getting a quick summary of the file object.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo