On 11/02/11 16:13, Matt wrote:
Hi All,
I am trying to do a really simple file operation, yet, it befuddles me...

I have a few hundred .csv files, and to each file, I want to manipulate the 
data, then save back to the original file. The code below will open up the 
files, and do the proper manipulations-- but I can't seem to save the files 
after the manipulation..

How can I save the files-- or do I need to try something else maybe with split, 
join, etc..


import os
import csv
for filename in os.listdir("/home/matthew/Desktop/pero.ngs/blast"):
        with open(filename, 'rw') as f:
                reader = csv.reader(f)
                for row in reader:
                        print ">",row[0],row[4],"\n",row[1], "\n",">", row[2], 
"\n", row[3]
Your last line just prints the data to standard-out.  You can 
either pipe the output to a file:
  python myprog.py > output.txt

or you can write them to a single output file:

  out = file('output.txt', 'w')
  for filename in os.listdir(...):
    with open(filename, 'rw') as f:
      reader = csv.reader(f)
      for row in reader:
        out.write(">%s%s\n%s\n>%s\n>%s\n%s" % (
          row[0], row[4], row[1], row[2], row[3]))

or you can write them to output files on a per-input basis:

  for filename in os.listdir(SOURCE_LOC):
    with open(filename, 'r') as f:
      outname = os.path.join(
        DEST_LOC,
        os.path.basename(filename),
        )
      with file(outname, 'wb') as out:
        for row in reader:
          out.write(">%s%s\n%s\n>%s\n>%s\n%s" % (
            row[0], row[4], row[1], row[2], row[3]))

-tkc





--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to