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

2011-01-16 Thread Jochen Schröder
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')
fp.close()
s = fp.read()
s += "I suck at Python and need help")
fp_new = open(fn[:-4]+'_modified.txt','w')
fp_new.write(s)
fp_new.close()


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:
http://wiki.python.org/moin/BeginnersGuide

Cheers
Jochen
>
>> 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
>

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


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

2011-01-14 Thread Benjamin Root
On Friday, January 14, 2011, 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")
>         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
>

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@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


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

2011-01-14 Thread Zachary Pincus
> 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

Yeah, it's probably the wrong list; still, easy enough to answer...

You want this:
   text_file = open(i, "a")

to open the individual file, as opposed to:
   text_file = open(textlist, "a")

which tries to open the whole list of filenames. Python cannot figure  
out how to turn the list into a single (unicode) filename, hence the  
error.

As for your wanting to write files with new names:

for txtfile in txtlist:
   f = open(txtfile, 'r')
   data = f.read()
   f.close()
   fnew = open(get_new_name(txtfile), 'w')
   fnew.write(data)
   fnew.write('\nHelp is on the way.')
   fnew.close()

where get_new_name() or whatever is defined appropriately to transform  
the old name ('test1.txt', say) into 'test1_appended.txt'...

Zach


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


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

2011-01-14 Thread dstaley

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

-- 
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@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion