开发者

directory and file related doubts?

开发者 https://www.devze.com 2023-01-01 23:10 出处:网络
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 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 hel开发者_开发知识库p

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消