[Pythonmac-SIG] Re-opening files?

Chris Barker cbarker@jps.net
Tue, 07 Mar 2000 16:08:39 -0800


Just van Rossum wrote:
> At 2:53 PM -0800 07-03-2000, Chris Barker wrote:
> >I am having a problem (more of an annoyance actually) when I try to
> >re-open a file that I am writing to. I get an erro when I do this:
> >
> >file = open('filename','wt')
> 
> Erm, what deos the 't' mean in the mode?

It means "text", except that Python defaults to text, and thus it means
nothing. It must be leftover from my other favorite high level language,
MATLAB. I never got an error, and old habits die hard.

> >
> >By the way, the above code works just fine on Linux. Python appears to
> >just re-wind the file (probably just the output buffer), without erasing
> >it. Curious behavior, but it works for me!
> 
> Hm, I don't know what the "official" behavior should be, but it doesn't
> seem unreasonable to me that you get an exception when opening a file for
> writing twice.

I agree that it seems reasonable, it's just pretty annoying. I use
Python a lot for short scripts, many of which are text file translators.
As a result, I run into this a lot. I suppose a feature that I would
like, and would solve this problem as well as a few others, is to have a
"Run Clean" option. This option would run the script in a cleaned-out
namespace, as though it were running as __main__ from scratch, just like
it will be when I run it outside of the IDE.

In fact, this is a Python, rather than a Mac Python question, but is
there a way to do something like a "del all", to get back to an empty
(built-ins only) namespace?

> I run into the same thing myself quite often, but I find it only a minor
> annoyance, and usually blame it on myself ;-) In case I get really annoyed,
> I do this:
> 
> f = open(filename, "w")
> try:
>     ...stuff that might crash...
> finally:
>     f.close()

Nice trick. I'll probably start doing that myself.


> >While I'm at it, other IDE questions:
> >
> >Is there a way to get the output window in the IDE to be interactive? I
> >often would like to use the command line after my script terminates to
> >check something out, but there is no way to do this.
> 
> ??? I don't follow. But: have a look at the little popupmenu at the top
> right corner of the interactive window, it has an entry called "Namespace"
> which offers a list of open source files. After slecting your
> script/module, the interactive window is executing commands in *that*
> namespace.

Well look at that!! I never noticed that little popup menu! Very handy!
Yes, that solves my problem.

> >Also, is there an easy way to change the working directory in the IDE
> >interactive window? import os, os.chdir('the whole path') is kind of a
> >pain.
> 
> There are two scripts in in the Scripts menu called "Insert file name" and
> "Insert folder name". Seems fairly convenient...

It turns out that my script directory got changed, so it took me a while
to find them, but now I have, and they are handy. I still think a
"change the working directory" item would be a handy thing to put in the
popup menu. Even better, have it automatically change to the working
directory of the script that you're working on.

Thanks for the tips, I'm starting to get comfortable working in the IDE
finally.

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
cbarker@jps.net                      ---           ---           ---
http://www.jps.net/cbarker          -----@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Water Resources Engineering       ------   @    ------   @   ------   @
Coastal and Fluvial Hydrodynamics -------      ---------     --------    
------------------------------------------------------------------------
------------------------------------------------------------------------