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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

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

    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
    [color=blue][color=green][color=darkred]
    >>> f=open('mytext. txt','w+')
    >>> f.write('My name is Bob')
    >>> s=f.read()
    >>> s.len()
    >>> len(s)[/color][/color][/color]
    4082[color=blue][color=green][color=darkred]
    >>>f.close()[/color][/color][/color]

    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

  • Thomas Jollans

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

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

    Comment

    • Steven D'Aprano

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

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

      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.

      Comment

      • Fredrik Lundh

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

        "Alex" wrote:
        [color=blue]
        > 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??[/color]

        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>



        Comment

        • Ksenia Marasanova

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

          18 Sep 2005 09:11:51 -0700, Alex <lidenalex@yaho o.se>:[color=blue]
          > Rossum's tutorial on Python states:[/color]

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

          --
          Ksenia

          Comment

          • Alex

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

            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)
            [color=blue][color=green][color=darkred]
            >>> f=open('mytext. txt', 'w+')
            >>> f.write('My name is Bob')
            >>> f.seek(0)
            >>> s=f.read()
            >>> print s[/color][/color][/color]
            My name is Bob[color=blue][color=green][color=darkred]
            >>> f.close()[/color][/color][/color]

            I've found this piece of clue at

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

            Comment

            • Alex

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

              Thanks Steven, very good explaination. f.seek(0) does the trick!
              Alex

              Comment

              Working...