tags:

views:

71

answers:

3

i have a directory with around 1000 files....i want to run a same code for each of these file...

my code requires the file name to be inputted.

i have written code to copy the information of one into other in other format... please suggest a method to copy all 1000 files one by one without need to change the file name every time

and i have a field serial_num which need to be continous i.e if 1st file has upto 30 then while coping other file it should continue from 30not from 0 again

require help

thanks..

from string import Template
from string import Formatter
import pickle
f=open("C:/begpython/wavnk/text0004.lab",'r')
p='C:/begpython/wavnk/text0004.wav'
f1=open("C:/begpython/text.txt",'a')
m=[]
i=0
k=f.readline()
while k is not '':
    k=f.readline()
    k=k.rstrip('\n')
    mi=k.split(' ')
    m=m+[mi]
    i=i+1

y=0
x=[]
j=1
t=(i-2)
while j<t:
    k=j-1
    l=j+1
    if j==120 or j==i:
       j=j+1
    else:
        x=[]
        x = x + [y, m[j][2], m[k][2], m[l][2], m[j][0], m[l][0], p]
        y=y+1
        #f1.writelines(str(x)+'\n')
        for item in x:
            f1.write(str(item)+'   ')
        f1.write(str('\n'))
        j=j+1

f.close()
f1.close()

my code.....

and i have files name in series like text0001.....text1500.lab and want to run them at a time without need to call them everytime by changin name

enter code here
+2  A: 

Why not just use an iterator over the list of files in the directory? I would post some example code but I do get the feeling that you're getting everyone else here to do your whole job for you.

Damian Kennedy
haha!!! gud joke...please dont just pass comments,before knowing the actuals...u dont know what i am workingand how could u say so directlyi have written the whole code myself..i am just asking help and points where i got err while runnin and have no clue for rectifying it..
kaushik
I was hesitant to upvote this because it's not really what I would consider an answer... but then I reread the OP's "question" and he literally only asked for a suggestion, which you've provided. I also completely agree with your feeling. Not to be spiteful of the OP, but I truly hope no one actually posts code for him until he demonstrates he's actually put forth some effort and is genuinely stuck. How will he learn otherwise?
John Y
@kaushik: You will probably want to review your question and your attitude.
Xavier Ho
i guess i proved my work can i get some code now??
kaushik
A: 

You can list the contents of the directory with [listdir][1].

You can the filter on extension with something like

allnames = listdir...
inputnames = [name for name in allnames \
              where os.path.[splitext][2](name)\[1\] == ".lab" ]

You can also look at the filter() or map() built-in functions.

http://docs.python.org/library/os.path.html#os.path.splitext

extraneon
A: 

You could take a look at the glob module as well. It's this easy:

import glob
list_of_files = glob.glob('C:/begpython/wavnk/*.lab')

And yes, it works on windows as well. However, it only finds the matching files, doesn't read them or anything.

By the looks of your code example, you may or may not be interested in the python csv module as well.

Mattias Nilsson
does this work for window??anything similar for windows this looks gud
kaushik