Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > File read from stdin and printed to temp file are not identicial?

Reply
Thread Tools

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

 
 
Jean Luc Truchtersheim
Guest
Posts: n/a
 
      09-16-2010
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[i] != line:
print >>sys.stderr, "line %d:\nfile(%d):\"%s\"\narray(%d):\"%s\"" %
(i+1, len(line), line, len(data[i]), data[i])
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.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-17-2010
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[i] != line:
> print>>sys.stderr, "line %d:\nfile(%d):\"%s\"\narray(%d):\"%s\"" %
> (i+1, len(line), line, len(data[i]), data[i])
> 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()
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      09-17-2010
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"
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      09-17-2010
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"
 
Reply With Quote
 
Jean Luc Truchtersheim
Guest
Posts: n/a
 
      09-17-2010
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.
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      09-17-2010
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
How to initialize member reference with temp object and delete the temp object when the destructor is called? PengYu.UT@gmail.com C++ 2 05-09-2005 05:48 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM
File::Temp: opening the temp. file in "r+" mode? Also "man in the middle" A. Farber Perl Misc 3 03-03-2004 12:42 PM
Temp Internet and temp files now in winnt directory w2k pro Bibble Bobble Computer Support 1 11-27-2003 06:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57