[Numpy-discussion] NOOB Alert: Looping Through Text Files...

Jochen Schröder cycomanic at gmail.com
Sun Jan 16 18:23:59 EST 2011

On 15/01/11 09:52, dstaley wrote:
> Warning, I am a python noob.  Not only do I not know python, I really don't
> know anything about programming outside of ArcInfo and the ancient AML
> language.  Regardless, here is my problem....
> Let's say I have three text files (test1.txt, test2.txt and test3.txt).
> Each text file has 1 line of text in it "This is my text file", to which I
> want to add (append) a new line of text saying "I suck at Python and need
> help", and then save the file with a suffix attached (eg test1_modified.txt,
> test2_modified.txt, test3_modified.txt).
> I guess this is the equivalent of making a change in MS Word and using the
> "Save As..." command to maintain the integrity of the original file, and
> save the changes in a new file.  But, I want to also do that in a loop (this
> is a simplified example of something I want to do with hundreds of text
> files).
> Now, I understand how to add this line to an existing text file:
> text_file = open("test1.txt", "a")
> text_file.write("\nI suck at Python and need help")
> text_file.close()
> While this adds a line of text, it saves the change to the original file
> (does not add the _modified.txt suffix to the file name), nor does it allow
> me to loop through all three of the files.
> I'm sure this is an easy thing to do, and is online in a million places.
> Unfortunately, I just cannot seem to find the answer.  Here is my thought
> process:
> First i would define the list from which I would loop:
> textlist = ["test1.txt", "test2.txt", "test3.txt"]
> for i in textlist:
> 	text_file = open(textlist, "a")

This is your problem.  You create the textfile list, and then loop over 
the list. Now the is are your elements of the list, however you are 
passing the list to the open function. That is what the error says, it 
expects a string but found a list. The better way to do this would be:

for filename in textlist:
          text_file = open(filename, "a")

> 	text_file.write("\nI suck at Python and need help")
> 	text_file.close()
> But, this doesn't work.  It gives me the error:
> coercing to Unicode: need string or buffer, list found
> SO, I guess I need to do this from something other than a list?
> Even if it did work, it does not suit my needs as it does not create a new
> file and does not allow me to add the _modified.txt suffix, which will allow
> me to keep the original file intact.

There are a number of ways to to do this and what the best way is might 
depend on your text files. If they are very short the easiest way is to 
just read the content of your text files and write the content to a 
different file. something like this:

for fn in textlist:
        fp = open(fn, 'r')
        s = fp.read()
        s += "I suck at Python and need help")
        fp_new = open(fn[:-4]+'_modified.txt','w')

Just for the future this is the numpy list, which is for discussing 
issues relating to the numpy python module, the next time you might want 
to post a question like this to one of the python beginners lists. This 
link might get you started:

>> From a responses to a previous post, this seems as if it may have something
> to do with a python dictionary, but I'm not sure.
> I'm probably totally off on how this should even be written, so any advice
> or suggestions would be greatly appreciated.
> Thanks in advance for your help!
> -DS

More information about the NumPy-Discussion mailing list