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

被折叠的 条评论
为什么被折叠?



