python 遍历文件夹子文件夹,批量生成txt

来源:互联网 发布:linux jar 解压 编辑:程序博客网 时间:2024/04/28 10:46

import osimport os.pathdef createList(root_dir, class_list, info_name):    info = open(info_name, "w")    for class_name, label in class_list:        class_path = root_dir + "/" + class_name        for name in os.listdir(class_path):            info.write(class_path + "/" + name + "\t" + str(label) + "\n")    info.close()        if __name__ == "__main__":    #class_list = [(str(i), i) for i in range(7)]   这个是文件夹名字为 1 2等情况#class_list = [('latin','latin')]# class_list = [('Arabic_0','Arabic_0')]info_name = os.getcwd()#print info_name#root_dir = os.getcwd()+"/Train"root_dir = os.getcwd()+"/Test"class_list = []FileName = []FileNames = os.listdir(root_dir)  这种方法文件夹名为任意值情况ErrorFileName = 'error' #error fileprint ErrorFileNamelabel = 0if(len(FileNames)>0):for fn in FileNames:print fn if fn != "error":  这里用于判断 error 文件夹不用批处理FileName = [fn,label]label = label + 1class_list.append(FileName)label = 0#info_name = os.getcwd() + "/train-list.txt" info_name = os.getcwd() + "/test-list.txt"createList(root_dir, class_list, info_name)    # root_dir = os.getcwd() + "/Train"    # info_name = os.getcwd() + "/train-list.txt"    # createList(root_dir, class_list, info_name)                           #RootDir = os.getcwd()+"/Test" # RootDir = os.getcwd() # for parent,dirnames,filenames in os.walk(RootDir):# for dirname in dirnames:# print "parent is:" + parent# print "dirname is" + dirname# for filename in filenames:# print "parent is:" + parent# print "filename is:" +filename# print "the full name of the file is:" + os.path.join(parent,filename)# RootDir = os.getcwd()+"/Test" # FileList = []# FileName = []# FileNames = os.listdir(RootDir)# if(len(FileNames)>0):# for fn in FileNames:# FileName = [fn,fn]# FileList.append(FileName)# print FileList


可以参考下边

import os
 
def GetFileList(dir, fileList):
    newDir = dir
    if os.path.isfile(dir):
        fileList.append(dir.decode('gbk'))
    elif os.path.isdir(dir):  
        for in os.listdir(dir):
            #如果需要忽略某些文件夹,使用以下代码
            #if s == "xxx":
                #continue
            newDir=os.path.join(dir,s)
            GetFileList(newDir, fileList)  
    return fileList
 
list = GetFileList('D:\\workspace\\PyDemo\\fas', [])
for in list:
    print e

0 0