writing output to file

Mark McEahern marklists at mceahern.com
Thu Jul 11 15:27:40 EDT 2002


> Inspired by an existing Perl script, I wrote this chunk of code,
> but instead of writing the result to the screen, I would like
> to write output to a another file.

f = file(output_filename, "w") # use "a" if you want to append
f.write(data) # to your heart's content
f.close()

You can also spell file as open, but file is preferred since that matches
the returned object's type.

Play around with the file descriptor in Python's interactive shell to see
other methods:

  $ python
  >>> f = file("junk", "w")
  >>> dir(f)

etc.

// m

-






More information about the Python-list mailing list