Reading and Writing

Sir Galahad the chaste sttmjoc0 at fastmail.fm
Thu Dec 23 07:28:13 EST 2004


Hi,

elias.goodman at gmail.com wrote:
> How should I: Open a Text file, read from it, modify it, print to
> another .txt?
> 
> For instance: Read a string, sort it, write the sorted string.

What do you mean by "sorting"? If you want to sort the lines contained 
in a file, you could do something like this.

$ cat in.txt
foo
bar
baz
ham
spam
$ cat process.py
#!/usr/bin/env python
lines = open("in.txt").readlines()
lines.sort()
out = open("out.txt", "w")
for line in lines:
     out.write(line)
out.close()
$ python process.py
$ cat out.txt
bar
baz
foo
ham
spam

Regards
G.



More information about the Python-list mailing list