[Tutor] Text processing with files
Intatia
intatia at paradise.net.nz
Sun Oct 26 00:09:04 EDT 2003
Here's my pathetic attempt with what little I know...:)
from os import linesep
filepath = raw_input("Convert which file? ")
fileread = open(filepath, "r")
text = fileread.readlines()
fileread.close()
fixedtext = []
for line in text:
fixedtext.append(line.replace('\n', linesep))
filewrite = open(filepath, "w")
filewrite.writelines(fixedtext)
filewrite.close()
Seems to work, or at least, the file keeps it's contents *g*
It's probably a matter of style, but I prefer to keep
opening/(reading/writing)/closing a file all together where possible for easier
reading.
Daniel Ehrenberg wrote:
> I'm trying to make a program to convert any text
> document into one with he native type of newline. I
> wrote a script but all it did was blank the file. It
> is posted below.
>
> from os import linesep
> filepath = raw_input("Convert which file? ")
> fileread = file(filepath, "r")
> filewrite = file(filepath, "w")
> text = fileread.read()
> fileread.close()
> fixedtext = text.replace('\n', linesep)
> filewrite.write(fixedtext)
> filewrite.close()
>
> What's wrong with it?
> Daniel Ehrenberg
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list