Problem Writing Files
Bengt Richter
bokr at oz.net
Wed Apr 17 00:28:07 EDT 2002
On Tue, 16 Apr 2002 19:39:56 -0700, Roger Jones <rmj at slac.stanford.edu> wrote:
>I am using ActiveState Python 2.1 under Windows NT and after opening a
>file with:
>
>afile=('test.dat','w')
>afile.write('some random test text')
>afile.read()
>
>It fills the file with many x's and 0's and also with various other
>text.
>How can this action be fixed so that it writes the text properly?
>
>Thanks!
>
Do you know how to copy from an interactive session and paste to your newreader
for posting? In the "DOS" window running Python, you can use Alt-Space>e>k
and then use the mouse to left-button-down on the top left character of a box area
that you create by dragging towards bottom right. When sized as desired, release
the mouse button and press Enter to copy to clipboard.
>From IDLE it's more normal windows text selection, so you should know.
For best response here, please try one of the above.
>>> afile=('test.dat','w')
>>> afile.write('some random test text')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'write'
>>> afile.read()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'read'
Suspicion is you were just theorizing, or didn't transcribe carefully ;-)
RTFD also applies:
>>> print file.__doc__
file(name[, mode[, buffering]]) -> file object
Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
writing or appending. The file will be created if it doesn't exist
when opened for writing or appending; it will be truncated when
opened for writing. Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
buffered, and larger numbers specify the buffer size.
Note: open() is an alias for file().
I'd do it for you, but thought you'd get more out of this ;-)
Maybe the next go won't work either, but things'll go faster if you
post the real thing.
Regards,
Bengt Richter
More information about the Python-list
mailing list