Problem Writing Files

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Wed Apr 17 23:10:35 EDT 2002


----- Original Message -----
From: "Jones, Roger M." <rmj at SLAC.Stanford.EDU>


> Dear Chris,
>
> Thank you for your reply.  It is helpful.
> I tried rewind and seek but they
> do not work properly.

First rule of asking for help via email or USENET:  post
lots of detail.  What exactly did you do?  Copy the actual
screen with the error message and paste it into your email.

For instance, this is the "right" way to write and then
re-read a file, copied from an actual session (you may need
to widen your window to read this):

----------------------------------------------------------------------------
-------
PythonWin 2.1.2 (#31, Jan 15 2002, 17:28:11) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH at ActiveState.com) - see
'Help/About PythonWin' for further copyright information.
>>> afile = open("myfile.txt", "w+")
>>> afile.write("a line of text.\n")
>>> afile.seek(0)
>>> afile.read()
'a line of text.\n'
>>> afile.seek(0)
>>> afile.readline()
'a line of text.\n'
>>> afile.write("another line.\n")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
IOError: (0, 'Error')
>>> afile.seek(0, 1)
>>> afile.write("another line\n")
>>> afile.seek(0, 0)
>>> afile.readline()
'a line of text.\n'
>>> afile.readline()
'another line\n'
>>> afile.readline()
''
>>>
----------------------------------------------------------------------------
-------

I open the file in "w+" mode (read the documentation for the
builtin open function), then write a line of text, then seek
the start of the file (read the file object docs in section
2.1 of the library documentation; 2.1.7.9 in version 2.1.2
manuals).  The seek is required on most platforms when
switching between write and read or read and write on a
stream.  Next I read the entire file (with the read method).

I attempt to write a line immediately (the file pointer is at
the end of file, so I expect I am appending a new line) but
I get an exception of type IOError because I neglected to
seek before changing modes.  Sheepishly I do a seek to 0
bytes from the current position (that's what the second
argument to seek does; read thos docs in the library manual
section 2.1!) and write the line.  The next seek is to the
start of file (I don't have to include the second argument
but I did anyway).

Now the readline method returns the first line, because that's
where the file pointer is right now.  Another readline gets
another line from the file; the null string after the third
readline is the end of file indicator.

> Is there a module
> that needs to be loaded before using rewind or seek?

Nope.  READ THOSE LIBRARY DOCS, especially the file object
documentation.

> You are right, I failed to type "open"
> which I of course used in the actual python session.

Yup.

Everything you need for basic understanding of file I/O
is in the example above.  Absorb it.

OK, I left one thing out:

>>> afile.close()

(Note that the >>> is the prompt from Pythonwin, where I
ran this test.)  Closing the file is not required, as
Python closes all open files automatically on exiting the
interpreter, but as each open file represents a resource
in use, and you don't want to run out, you should close
file handles (file objects) explicitly when you don't need
them anymore.

Hope this helps.

Oh, yeah, READ THE MANUALS.

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net






More information about the Python-list mailing list