[Courses] [python] No lesson -- open discussion

Leslie leslie.brothers at verizon.net
Sun Aug 21 20:51:01 UTC 2011

> Anybody do any fun or interesting Python hacking recently?
> Or have any Python questions?
	First, thanks for this excellent course!
	I do have a question about file I/O.
	I had the objective of opening a file containing several lines, and
adding lines to it.  How I solved this is probably awkward. Is there a
better procedure or any general comments you have about working with
I start with myfile.txt which has 2 lines:

This is line 1 of my file.
This is line 2 of my file.

Then, interactively:
>>> import os, subprocess, sys
>>> mylist = ["This is line 3 of my file.\n", "This is line 4 of my
>>> f = open("myfile.txt", "a")
>>> for i in mylist:
...     f.write(i)
>>> f.close()
>>> x = subprocess.Popen(["cat","myfile.txt"], stdout=subprocess.PIPE)
>>> for line in x.stdout:
...     print line
I get what I'm expecting (I closed the spaces to make this shorter),

This is line 1 of my file.
This is line 2 of my file.
This is line 3 of my file.
This is line 4 of my file.

Although it works, I am not sure this is the best way to add lines from
a list.  And if I wanted to append one file to another, is there a
straightforward way?


> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses

More information about the Courses mailing list