[Tutor] files in a directory

Jay Loden python at jayloden.com
Sun Jan 30 09:14:08 CET 2005


There's a few ways to accomplish this...the way that comes to mind is: 

##########################################################
import glob

files = glob.glob("/path/to/director/*.dml")  # assuming you want only .dml 

def spot(file):
  '''search for intensity spots and report them to an output file'''
  f1 = open('my_intensity_file.dml','r')
  int = f1.read().split('\n')

  my_vals  = intParser(int) 

  intParser return a list
  f2  = open('myvalues.txt','w') # you will want to change this to output mult 
  for line in my_vals:   # files, or to at least append instead of overwriting
      f2.write(line)
    f2.write('\n')
  f2.close()

def main():
  for each in files:
    spot(each)

main()

##########################################################

Basically, turn the parsing into a function, then create a list of files, and 
perform the parsing on each file.  glob() lets you grab a whole list of files 
matching the wildcard just like if you typed "ls *.dml" or whatever into a 
command prompt.  There wasn't too much info about specifically how you needed 
this to work, so this is a rough sketch of what you want. Hopefully it helps.

-Jay

On Sunday 30 January 2005 03:03 am, kumar s wrote:
> Hello.
>
> I wrote a parser to parse spot intensities. The input
> to this parser i am giving one single file
>
> f1 = open('my_intensity_file.dml','r')
> int = f1.read().split('\n')
>
> my_vals  = intParser(int)
>
> intParser return a list
> f2  = open('myvalues.txt','w')
> for line in my_vals:
>      f2.write(line)
>      f2.write('\n')
>
> f2.close()
>
>
> The problem with this approach is that, i have to give
> on file per a run. I have 50 files to pare and i want
> to do that in one GO.  I kepy those 50 files in one
> directory. Can any one suggest an approach to automate
> this process.
>
> I tried to use f1 = stdin(...) it did not work. i dont
> know , possible is that i am using incorrect syntax.
>
> Any suggestions.
>
> Thank you.
> K
>
>
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> All your favorites on one personal page – Try My Yahoo!
> http://my.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list