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

本文介绍了一个Python脚本,该脚本用于遍历指定目录下的所有子目录及文件,并将文件路径及其对应的标签写入到一个文本文件中。此方法适用于数据集的整理,特别是图像分类任务的数据准备。

import os

import os.path

def 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 file

print ErrorFileName

label = 0

if(len(FileNames)>0):

for fn in FileNames:

print fn

if fn != "error": 这里用于判断 error 文件夹不用批处理

FileName = [fn,label]

label = label + 1

class_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 sin 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 ein list:

print e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值