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

Steven D'Aprano steve at
Sun Sep 18 19:21:15 CEST 2005

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.len()
>>>> len(s)
> 4082
> 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.write('My name is Bob')
s =
# s should be the empty string -- untested because I'm not running Windows
s =
# s should be "My name is Bob"

Hope this helps.


More information about the Python-list mailing list