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

  • Thread starter Jean Luc Truchtersheim
  • Start date
J

Jean Luc Truchtersheim

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

MRAB

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()
 
J

James Mills

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
 
J

James Mills

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
 
J

Jean Luc Truchtersheim

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

James Mills

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top