View Full Version : Opening a file using Python
Mallet
July 11th, 2009, 02:27 PM
I'm having a problem with mechanize.LWPCookieJar load and save. I've narrowed it down to the code which is opening the file.
Can someone verify whether the following works:
f = open('test.txt', 'w')
I get an "invalid mode: w" error while executing this within Boxee, but outside of Boxee there's no problem.
DPK
July 11th, 2009, 02:42 PM
f = open('/tmp/workfile', 'w')
...should work.
http://docs.python.org/library/functions.html#open
Mallet
July 12th, 2009, 11:06 AM
Oops. The code I'm troubled with is just as you've suggested.
def save(self, filename=None, ignore_discard=False, ignore_expires=False):
if filename is None:
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
f = open(filename, "w")
try:
debug("Saving LWP cookies file")
# There really isn't an LWP Cookies 2.0 format, but this indicates
# that there is extra information in here (domain_dot and
# port_spec) while still being compatible with libwww-perl, I hope.
f.write("#LWP-Cookies-2.0\n")
f.write(self.as_lwp_str(ignore_discard, ignore_expires))
finally:
f.close()
As I stated in my orginal post, I get "invalid mode: w" exception.
waltermoore
July 12th, 2009, 11:29 AM
Are you using GetTempDir() (http://developer.boxee.tv/python-api-reference/#mc) so you have the right temp dir?
What OS are you using? Does the same thing happen in Win/Mac/Linux?
A Google search on your error reveals that some platforms can produce cryptic error messages with open() in Python 2.4 This can happen when the directory or file does not exist, and/or when there are character encoding/decoding issues in the contents of the file.
Google Search for Python 2.4 open() invalid mode 'w' (http://www.google.com/search?hl=en&hs=0E2&q=python+2.4+open()+invalid+mode+%27w%27&aq=f&oq=&aqi=)
Hope this helps...
-Walter
Mallet
July 14th, 2009, 12:31 PM
Using GetTempDir() yields the same results. I'm developing on Windows and can't speak for Mac and Linux.
As a work around, I'm writing the file contents to an entry in the local config.
Powered by vBulletin® Version 4.1.12 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.