[Tutor] file i.o

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jan 25 03:22:32 EST 2004


> i'm unable to write to a file, i can read and print to the shell but
> only empty files are created. for a quick example below, if i import *
> from os i get the following error.

Hi Kim,


Don't do that.  *grin*

os.open() is a low-level function: it's not meant to be used unless you
really know what you're doing.  Most Python programmers will never touch
that particular function.

(For reference: Python's os.open() is similar to Perl's sysopen()
function.)


In general, try to avoid the statement 'from some_module import *' in
Python unless the module documentation says that it's ok.  The problem is
that it clobbers the local namespace with all the names in the module, and
that's usually a bad thing.


The 'os' module, in particular, isn't a good candidate for that technique,
because it overrides the standard 'open()' function that you normally
should use.  The Python tutorial talks more about the problems with 'from
some_module import *':

    http://www.python.org/doc/tut/node8.html#SECTION008410000000000000000

near the end of section 6.4.1.


So let's take out that 'from os import *' statement, and look at the rest
of your program:

> output = open('numbers','wa')
                           ^^

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.


> counter = 0
> while (counter<10):
>      output.write("%f\n" % (counter))
>      print counter
>      counter = counter + 1

Ok, this looks fine.  You need to add one more statement at the end: the
file needs to be 'close()'ed for the content to be saved into the file:

    output.close()

should do the trick.  Closing a file properly clears out the buffers that
the operating system keeps in memory, and this ensures that the content is
saved to disk.  (In Perl, too, you should make sure to close any open file
handles to make sure the buffers flush properly.)



> one other question, what would the python equivalent of this (below)
> be? i.e executing another programs and grabbing the output straight into
> a list. I haven't been able to find out how to do this, but i'm sure
> there is a way.
>
> open (XLOGP, "/usr/local/dock/bin/xlogp dock_nrg.mol2 |");
> $xlogp =<XLOGP>;

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()
###

to print out the contents of the current directory as a large list.



By the way, in Perl, you may be used to doing something like:

### Perl
while (my $line = <FILEHANDLE>) {
    ## do something with the line.
}

to handle a file line-by-line.

There's an equivalent statement in Python:

### 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.

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:

    http://www.python.org/doc/tut/


If you have more questions, please feel free to ask!  Good luck to you.




More information about the Tutor mailing list