[Tutor] Creating files inside a script [I made a mistake!]

Sheila King sheila@thinkspot.net
Mon, 23 Apr 2001 21:13:09 -0700


On Sun, 22 Apr 2001 23:48:57 -0700 (PDT), Daniel Yoo
<dyoo@hkn.eecs.berkeley.edu>  wrote about Re: [Tutor] Creating files inside a
script [I made a mistake!]:

:On Sun, 22 Apr 2001, Sheila King wrote:
: 
:> This is how I would have thought things work, and that is obviously what the
:> docs say. But, then I just don't get why I was getting this error:
:> 
:>   File "gypsymail.py", line 117, in PrintErrorToLogFile
:>     logfile = open(logfilename, 'a+')
:> IOError: (2, 'No such file or directory')
:
:Very strange...  It sounds like it might be something platform dependent,
:because Python's open() is definitely using the C version of fopen() that
:your system provides.
:
:If you can tell us what kind of system you're on, we can verify that it's
:a platform-specific issue.  

Here is the info about the server:
Linux Kernel: 2.2.19-RAID-FQ_Phoenix-sr91
(RedHat...I believe 6.2, but upgraded/rebuilt kernel several times since?)

:The Python docs hint as much when they talk
:about how append might be wacky under certain Unices.
:
:    http://python.org/doc/current/lib/built-in-funcs.html
:
:"... and 'a' opens it for appending (which on some Unix systems means that
:all writes append to the end of the file, regardless of the current seek
:position)."
:
:Go figure.

Yeah, I went there and read that over (I'm sure I've read it before, too...),
but this time I especially looked for hints about wacky OS stuff, but I really
don't see that...

:
:> In any case, the script seems to be working fine, now. I changed that line of
:> my program to 
:>     logfile = open(logfilename, 'a')
:> 
:> since I'm only going to write to the file, anyway, and I don't need
:> the a+ functionality. It now seems to create the file, when the file
:> doesn't already exist. Beats me, what the problem was before.
:
:If we still want "a+" behavior, and have it do it nicely, we can write a
:small wrapper function that does the same thing as open(), but test things
:out:
:
:###
:def safe_aplus_open(filename):
:    """A "permissive" style open() for mode "a+".
:
:It appears that on some Unices, if the file doesn't exist,
:we run into IOErrors.  This function will try to open it
:with "a+", and if it fails, creates the file, and tries again.
:"""
:    try:
:        return open(filename, "a+")
:    except IOError:
:        open(filename, "w")
:        return open(filename, "a+")
:###
:
:
:I wonder if there's a better way to do this.  Until then, if you need
:"a+", this function should make things nicer.

Interesting. I would have never thought of that. I probably don't need a+
mode, but who knows what may come up in the future?

:By the way, have you been able to find a Python user's group in SoCal?  
:Just curious.

Well, no. Although I've made, really, no effort, other than the inquiry I made
on this list. (Heh, Diedre's suggestion that *I* could start one, certainly
scared me enough to make me shut up! At this point in my life, there is no way
I could take on, yet one more "thing to do"! Maybe some time in a few months
from now? Although I don't know where we would hold the meetings. My house is
totally inappropriate.)

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/