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

Benjamin Root ben.root at ou.edu
Sat Jan 15 00:16:02 EST 2011

On Friday, January 14, 2011, dstaley <dstaley at usgs.gov> 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")
>         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.
> >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


First, the problem with your loop is that you should pass 'i' not
'textlist' to the call to open.

Second, to do what you want, you need to copy the file (maybe
something in is.sys?) and then append the text to that copied file.

I hope that helps

> --
> View this message in context: http://old.nabble.com/NOOB-Alert%3A-Looping-Through-Text-Files...-tp30676099p30676099.html
> Sent from the Numpy-discussion mailing list archive at Nabble.com.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list