Output File
Steven Bethard
steven.bethard at gmail.com
Fri Feb 25 01:04:48 EST 2005
Samantha wrote:
> 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.
More information about the Python-list
mailing list