Output File

S

Samantha

Is there a limit on the size of the file Python will read then output. I am
reading a file of 433 lines and when I output the same file it only will
output 421 lines. The last line is cut off also. This is the code I am using
as a test.

input = open(r'C:\Documents and Settings\Owner\Desktop\somefile.html','r')
L = input.readlines()
input.close

output = open(r'C:\Documents and
Settings\Owner\Desktop\somefile_test.html','w')
for t in range(len(L)):
output.writelines(L[t])
output.close

Also is there a way to test for EOF in Python?
Thanks
S
 
S

Steven Bethard

Samantha said:
input = open(r'C:\Documents and Settings\Owner\Desktop\somefile.html','r')
L = input.readlines()
input.close

output = open(r'C:\Documents and
Settings\Owner\Desktop\somefile_test.html','w')
for t in range(len(L)):
output.writelines(L[t])
output.close

I think you want to do [1]:

input = open(r'somefile.html', 'r')
lst = input.readlines()
input.close() # note the () -- this is a method call

output = open(r'somefile_test.html', 'w')
output.writelines(lst) # not in a for-loop
output.close() # note the () -- this is a method call

If you really want to use a for-loop, the code should look like:

for line in L:
output.write(line)

If you call writelines when you only want to write one line, you're
going to get odd behavior -- Python's going to interpret each character
in your line as a "line" itself.
Also is there a way to test for EOF in Python?

file.read() or file.readline() will return '' if you have reached the
end of the file.

STeVe

[1] In fact, what you really probably want to do is to take advantage of
the fact that a file is an iterator. You can write:

input = open(r'somefile.html', 'r')
output = open(r'somefile_test.html', 'w')
output.writelines(input)

And the lines of somefile.html will be written to somefile_test.html.
You might also look at the shutil module.
 
S

Samantha

Thanks Steve. Appreciate it!
S
Steven Bethard said:
Samantha said:
input = open(r'C:\Documents and
Settings\Owner\Desktop\somefile.html','r')
L = input.readlines()
input.close

output = open(r'C:\Documents and
Settings\Owner\Desktop\somefile_test.html','w')
for t in range(len(L)):
output.writelines(L[t])
output.close

I think you want to do [1]:

input = open(r'somefile.html', 'r')
lst = input.readlines()
input.close() # note the () -- this is a method call

output = open(r'somefile_test.html', 'w')
output.writelines(lst) # not in a for-loop
output.close() # note the () -- this is a method call

If you really want to use a for-loop, the code should look like:

for line in L:
output.write(line)

If you call writelines when you only want to write one line, you're going
to get odd behavior -- Python's going to interpret each character in your
line as a "line" itself.
Also is there a way to test for EOF in Python?

file.read() or file.readline() will return '' if you have reached the end
of the file.

STeVe

[1] In fact, what you really probably want to do is to take advantage of
the fact that a file is an iterator. You can write:

input = open(r'somefile.html', 'r')
output = open(r'somefile_test.html', 'w')
output.writelines(input)

And the lines of somefile.html will be written to somefile_test.html. You
might also look at the shutil module.
 

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
474,221
Messages
2,571,131
Members
47,747
Latest member
swapote

Latest Threads

Top