[Tutor] file i.o

Kim Branson kim.branson at csiro.au
Sun Jan 25 03:51:41 EST 2004


>
> Hi Kim,
>
>
>
> Don't do that.  *grin*
>
i see, that makes sense now.
so one should always do a import string rather than from string import *
is there a way of warning about clobbering the namespaces ? so if i 
write my own module i can drag it in without worrying about mixing and 
matching module functions. the module.function() syntax could get a bit 
tedious, or is that part of the python way?
> The mode here is a little odd.  'w' is meant to destructively clear 
> out a
> file and prepare it for writing, and 'a' is meant to nondestructively
> "append" to an existing file.  That is, the two modes aren't supposed 
> to
> be compatible.  So choose one, not both.  *grin* To tell the truth, I 
> have
> no idea which mode will win out here.
>

oops sorry i was playing with it. i was trying append (a right) and 
creation. i'd like a if it exists append to the file otherwise make it. 
not closing filehandle *doh* makes sense now.  I would have thought 
file handles were closed on completion. bad perl coding style showing 
through. (part of the reason i'm learning python)
> os.popen() is the function you're probably looking for: it returns a
> file-like object.  So you can do something like:
>
> ###
> import os
> listing = os.popen("ls")
> print listing.readlines()
> ###
>
great. thats exactly what i want. even works for me first time.
> ### Python
> for line in filehandle:
>     # do something with the file.
> ###
>
> A 'filehandle' in Python can be treated as a "iterable" object, so the 
> for
> loop works on it beautifully.
>
neat.

> It does sound like you've had some programming under your belt; you may
> find the official Python Tutorial useful to get up to speed:
i have a copy of the oreilly learning python, but i've been told the 
essential ref might be good also. any other books i should look for ?
>
> If you have more questions, please feel free to ask!  Good luck to you.
thanks for your help, i'm sure there will be more.
cheers
Kim
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3664 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040125/e6704ddf/attachment.bin


More information about the Tutor mailing list