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
s
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
e
in
list
:
print
e
0 0
- python 遍历文件夹子文件夹,批量生成txt
- python 遍历文件夹 文件
- MATLAB读取txt文件,批量生成txt文件
- php 批量生成html、txt文件
- python 遍历文件夹及文件
- Python 批量合并多个txt文件
- python实现对于多个文件夹下的txt文件的特定字段进行提取,并生成新的txt
- python-遍历文件夹与递归遍历文件
- WPF 批量读取文件夹下TXT文件写入EXCEL
- Excel VBA - 遍历某个文件夹中文件、文件夹及批量建立txt
- Excel VBA - 遍历某个文件夹中文件、文件夹及批量建立txt
- 利用遍历查找输出文件夹下的txt后缀文件
- hadoop 遍历hdfs文件夹列出其中所有的txt文件
- 【批处理脚本】遍历文件夹内所有文件名到txt文件
- php遍历输出文件夹下所有txt文件
- 使用Python批量重命名文件夹中的文件
- python生成指定文件名的txt文件
- python方法生成txt标签文件代码
- java中的equals方法+hashCode方法
- Eclipse 导入项目乱码问题
- 常规功能和模块自定义系统 (cfcmms)—025开发日志(创建iconCls的field)
- aidl
- 乡村爱情故事8 下载地址
- python 遍历文件夹子文件夹,批量生成txt
- OC-045.NSSet类和NSMutableSet类
- JavaScript基础——事件
- Kinect for Windows SDK 1.6的改进及新特性
- iOS CocoaPods安装和使用图解
- Swift-常量变量
- Linux下的tar压缩解压缩命令详解
- Android签名
- box-shadow