471,312 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

file reading anomaly

Python 2.5, Windows XP.

I have a 48-line text file written by a Windows python script,

I try to read it as follows:

f = open ("depstats.txt", "r", 0)
for index, line in enumerate(f):
print index, len(line), len(line.split())
f.close()

On one PC, this runs without any problem.

On another, it appears to succeed (no run-time errors) but does not
read all the lines. If I print the lines and sum of the line lengths
so far inside the loop, the program can be seen to have only read the
first N bytes, where N is either 4096, or 8192.

Any ideas?

Thanks,

Gerry

Jul 6 '07 #1
2 1155
Update:

The PC seems to not be a factor (same problem exist on two PCs).

The entire script does some computation and writes depstats.txt.
The code snippet below attempts to read depstats.txt and write another
file. When the code below is run stand-alone, it succeeds. When run
in-line as part of the program that wrote depstats.txt, there are
three possible outcomes:

it stops reading after 4096 bytes (not counting \r's); no error
messages
it stops reading after 8192 bytes (not counting \r's); no error
messages
or it succeeds.

The first two are much more likely.

Trying buffersize parameters of -1, 0, 1, 1000, 8192, 16384 in the
open call for m all had no apparent effect.

Any ideas what could possibly be wrong earlier to cause this?
m = open("depstats.txt", "r", 20000)
n = open("newds.txt", "w")

linesout = 0
bytes = 0

for line in m:
bytes += len(line)
print len(line), len(line.split()), bytes
t = line.split()
if len(t) < 13:
print "not enough fields, only", len(t)
print t
sys.exit()
while len(t) < 43:
t.append(0)
t.append("F2")
t = [str(x) for x in t]
print >n, " ".join(t)
linesout += 1

print "linesout", linesout

m.close()
n.close()

On Jul 6, 12:48 pm, Gerry <gerard.bl...@gmail.comwrote:
Python 2.5, Windows XP.

I have a 48-line text file written by a Windows python script,

I try to read it as follows:

f = open ("depstats.txt", "r", 0)
for index, line in enumerate(f):
print index, len(line), len(line.split())
f.close()

On one PC, this runs without any problem.

On another, it appears to succeed (no run-time errors) but does not
read all the lines. If I print the lines and sum of the line lengths
so far inside the loop, the program can be seen to have only read the
first N bytes, where N is either 4096, or 8192.

Any ideas?

Thanks,

Gerry

Jul 6 '07 #2
Final update: problem solved. In the big script, the snippet was
reading depstats.txt which hadn't been closed.

Sorry for all the bother.

Gerry

Jul 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.