How does f=open('mytext.txt', 'w+') work?

A

Alex

Rossum's tutorial on Python states:
"open() returns a file object, and is most commonly used with two
arguments: 'open(filename, mode)'
mode 'r+' opens the file for both reading and writing."

Here's a little session in Python's interactive window

If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??

Alex
 
T

Thomas Jollans

I have no idea what is happening, but to the subject line: I guess it's
a plain wrapper around fopen fron <stdio.h>
 
F

Fredrik Lundh

Alex said:
If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??

you're moving the file pointer around in a new file, and you're getting
junk (from the stdio file buffers, most likely) in the places where you
haven't written anything yourself.

</F>
 
K

Ksenia Marasanova

18 Sep 2005 09:11:51 -0700 said:
Rossum's tutorial on Python states:

it's "Van Rossum's" :)
"van" in a part of the last name, you can't just cut it away in Dutch :)
 
S

Steven D'Aprano

Rossum's tutorial on Python states:
"open() returns a file object, and is most commonly used with two
arguments: 'open(filename, mode)'
mode 'r+' opens the file for both reading and writing."

Here's a little session in Python's interactive window


If I open the file mytext.txt in Notepad I see something that begins
with

"My name is Bob VwMÚ¸x¶ Ð"

and goes on for approximately 4082 characters.

What's happening??

4082 is exactly 14 bytes less than four kilobytes. The string you wrote
to the file is... 14 bytes long.

Seems to me the file system allocated a 4K block to your file. You wrote
14 bytes to it, which advances the file pointer to byte 14, and then
read to the end of the file, which was filled with whatever random bytes
just happened to be on the disk in that place.

I don't get this behaviour under Linux, so I assume this is
Windows-specific. Under Linux, the new file is created with length 0, and
read() returns the empty string.

Any file has a "physical length" (how many blocks allocated on disk) and a
"logical length" (how many bytes are actually used). You should expect
that any time you create a new file, the initial contents could be
anything until you over-write it. This is not a problem when you create a
new file in ordinary write mode, because you can't read those existing
bytes, and when you close the file, that specifies the end-of-file.

You might find the truncate() method useful:

f=open('mytext.txt','w+')
f.write('My name is Bob')
f.truncate()
s = f.read()
# s should be the empty string -- untested because I'm not running Windows
f.seek(0)
s = f.read()
# s should be "My name is Bob"
f.close()

Hope this helps.
 
A

Alex

Yes the problem seems to be exactly that. I'm moving around the file
pointer. This piece of code works. Observe the only thing I've added is
f.seek(0)

I've found this piece of clue at
http://msdn.microsoft.com/library/d...y/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp
"However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind operation. The current
position can be specified for the fsetpos or fseek operation, if
desired."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top