File read from stdin and printed to temp file are not identicial?

Discussion in 'Python' started by Jean Luc Truchtersheim, Sep 17, 2010.

  1. Hello,

    I am trying to read from stdin and dump what's read to a temporary
    file. My code works for small files but as soon as I have a file that
    has, e.g., more than 300 lines, there is always one and only one line
    that is truncated compared to the input.

    Here is my code:
    #---------------------------------------------------------------------------------
    #! /usr/bin/env python

    import sys
    from tempfile import *

    if __name__ == "__main__":
    data = []
    f_in = NamedTemporaryFile(suffix=".txt", delete=False)
    for line in sys.stdin:
    f_in.write(line)
    data.append(line)
    f_in.close
    f = open(f_in.name, 'rb')
    i=0
    for line in f:
    if data != line:
    print >>sys.stderr, "line %d:\nfile(%d):\"%s\"\narray(%d):\"%s\"" %
    (i+1, len(line), line, len(data), data)
    i += 1
    sys.exit()
    #-------------------------------------------------------------------------------------------------

    I feel that I must be doing something very stupid, but I don't really
    know what.

    Any idea?

    Can anybody reproduce this behavior.

    Thanks a bunch for any help.

    Jean Luc.
     
    Jean Luc Truchtersheim, Sep 17, 2010
    #1
    1. Advertising

  2. Jean Luc Truchtersheim

    MRAB Guest

    On 17/09/2010 00:36, Jean Luc Truchtersheim wrote:
    > Hello,
    >
    > I am trying to read from stdin and dump what's read to a temporary
    > file. My code works for small files but as soon as I have a file that
    > has, e.g., more than 300 lines, there is always one and only one line
    > that is truncated compared to the input.
    >
    > Here is my code:
    > #---------------------------------------------------------------------------------
    > #! /usr/bin/env python
    >
    > import sys
    > from tempfile import *
    >
    > if __name__ == "__main__":
    > data = []
    > f_in = NamedTemporaryFile(suffix=".txt", delete=False)
    > for line in sys.stdin:
    > f_in.write(line)
    > data.append(line)
    > f_in.close
    > f = open(f_in.name, 'rb')
    > i=0
    > for line in f:
    > if data != line:
    > print>>sys.stderr, "line %d:\nfile(%d):\"%s\"\narray(%d):\"%s\"" %
    > (i+1, len(line), line, len(data), data)
    > i += 1
    > sys.exit()
    > #-------------------------------------------------------------------------------------------------
    >
    > I feel that I must be doing something very stupid, but I don't really
    > know what.
    >
    > Any idea?
    >
    > Can anybody reproduce this behavior.
    >
    > Thanks a bunch for any help.
    >
    > Jean Luc.


    You're not closing f_in. That line should be:

    f_in.close()
     
    MRAB, Sep 17, 2010
    #2
    1. Advertising

  3. Jean Luc Truchtersheim

    James Mills Guest

    On Fri, Sep 17, 2010 at 9:36 AM, Jean Luc Truchtersheim
    <> wrote:
    > Can anybody reproduce this behavior.


    Jean it would help if you could provide samples of
    your input and output files. I'm pretty sure I might
    have a clue as to what your problem might be, but
    I can't be sure until I see the input and resulting output
    files.

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Sep 17, 2010
    #3
  4. Jean Luc Truchtersheim

    James Mills Guest

    On Fri, Sep 17, 2010 at 10:06 AM, MRAB <> wrote:
    > You're not closing f_in. That line should be:


    Although this _could_ be the problem (buffers not being flushed and
    the file being properly closed, etc)
    it could be something else...

    --James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Sep 17, 2010
    #4
  5. Dear Fellow python users,

    Many thanks for your help.

    Those missing brackets were the cause of my problem.

    Now my program works as expected.

    Many, many heartfelt thanks.
     
    Jean Luc Truchtersheim, Sep 17, 2010
    #5
  6. Jean Luc Truchtersheim

    James Mills Guest

    On Fri, Sep 17, 2010 at 10:25 AM, Jean Luc Truchtersheim
    <> wrote:
    > Dear Fellow python users,
    >
    > Many thanks for your help.
    >
    > Those missing brackets were the cause of my problem.
    >
    > Now my program works as expected.
    >
    > Many, many heartfelt thanks.


    Glad to hear it! Do you understand why ?

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Sep 17, 2010
    #6
    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. Replies:
    2
    Views:
    594
    Mark P
    May 9, 2005
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    824
    Dan Pop
    Jun 21, 2004
  3. Miner Jeff
    Replies:
    2
    Views:
    353
    Miner Jeff
    Nov 10, 2008
  4. A. Farber
    Replies:
    3
    Views:
    301
    Ben Morrow
    Mar 3, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    281
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page