April 5th, 2005, 02:22 PM
File not getting closed in Python ('W') mode
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
Please help me
April 5th, 2005, 02:57 PM
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.
April 5th, 2005, 03:48 PM
When I use your suggestion, what exactly is the second argument for pickle?
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.
April 6th, 2005, 02:57 PM
Originally Posted by mawe
I got some other options.
April 6th, 2005, 04:57 PM
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.
This method is also very useful for getting a quick summary of the file object.
>>> aFile = file('/Users/Mark/Desktop/sample.txt', 'w')
<open file '/Users/Mark/Desktop/sample.txt', mode 'w' at 0x4a4a0>
<closed file '/Users/Mark/Desktop/sample.txt', mode 'w' at 0x4a4a0>