By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
f=open('mytext.txt','w+')
f.write('My name is Bob')
s=f.read()
s.len()
len(s) 4082f.close()


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

Sep 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I have no idea what is happening, but to the subject line: I guess it's
a plain wrapper around fopen fron <stdio.h>
Sep 18 '05 #2

P: n/a
On Sun, 18 Sep 2005 09:11:51 -0700, Alex wrote:
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
f=open('mytext.txt','w+')
f.write('My name is Bob')
s=f.read()
s.len()
len(s) 4082f.close()


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

Sep 18 '05 #3

P: n/a
"Alex" wrote:
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>

Sep 18 '05 #4

P: n/a
18 Sep 2005 09:11:51 -0700, Alex <li*******@yahoo.se>:
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 :)

--
Ksenia
Sep 18 '05 #5

P: n/a
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)
f=open('mytext.txt', 'w+')
f.write('My name is Bob')
f.seek(0)
s=f.read()
print s My name is Bob f.close()


I've found this piece of clue at
http://msdn.microsoft.com/library/de...c_._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."

Sep 18 '05 #6

P: n/a
Thanks Steven, very good explaination. f.seek(0) does the trick!
Alex

Sep 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.