Trouble with file.seek/file.tell on Win32?

Discussion in 'Python' started by Prabhu Ramachandran, Aug 15, 2004.

  1. Hi,

    I noticed peculiar behavior under Python-2.3.4 under Win32. When I
    run something like this:

    f = open('t.txt', 'wb')
    f.write('1\012'+'2\012'+'3\012')
    f.close()
    f = open('t.txt', 'r')
    f.readline()
    pos = f.tell()
    val = f.read(1)
    f.seek(pos)
    assert val == f.read(1)

    I get an assertion error at this point. Everything works fine if I
    read the file with 'rb' instead of 'r'. But I can almost swear that
    this used to work earlier. I suspect that this behavior arises due to
    recent changes in fileobject.c (specifically, v2.187) where universal
    newline support was removed.

    http://cvs.sf.net/viewcvs.py/python/python/dist/src/Objects/fileobject.c?rev=2.192&view=log

    Could someone please clarify if this is this a bug or not? I would
    like to think that using tell and seek should work reliably enough for
    text files.

    The reason this is important for me is that I've been generating text
    files (with gay abandon) for MayaVi (http://mayavi.sf.net) for three
    years now. I always open the file using 'r'/'w' and not 'rb'/'wb'.
    Everything used to work fine. It never mattered where and how exactly
    the files were generated. Now, I get strange errors since I can no
    longer seek into these files reliably. I could switch to using
    'rb'/'wb' universally but all existing files will no longer work
    without either requiring a fair bit of effort on my part or on the
    part of users changing their saved files. I am loathe to ask every
    user to change all their files and change the newlines.

    Thanks.

    cheers,
    prabhu

    p.s. Please CC me in on replies. I'm not tracking c.l.py. Thanks.
     
    Prabhu Ramachandran, Aug 15, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Peters
    Replies:
    0
    Views:
    607
    Tim Peters
    Aug 15, 2004
  2. Prabhu Ramachandran

    Re: Trouble with file.seek/file.tell on Win32?

    Prabhu Ramachandran, Aug 16, 2004, in forum: Python
    Replies:
    2
    Views:
    500
    Christos TZOTZIOY Georgiou
    Aug 16, 2004
  3. N. Volbers
    Replies:
    3
    Views:
    361
    =?iso-8859-1?q?Lars_Gust=E4bel?=
    Jun 30, 2005
  4. Ben Temperton
    Replies:
    1
    Views:
    254
    Emile van Sebille
    Jun 14, 2012
  5. Replies:
    3
    Views:
    130
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page