1.新建一个txt文件,有读写功能
f = open("mapping2.txt", "w+")
f.write(str(mapping))
f.close()
2. 用于判断一个文件是否存在,不存在则新建一个
if not os.path.exists(new_folder):
os.mkdir(new_folder)
3.一个文件夹中有多个子文件夹,每个子文件夹中有多个文件,遍历每个子文件夹的内容
# encoding:utf-8
import os
import shutil
path1="/home/xionglin/274G2/face0413/dataset"
dst_path="/home/xionglin/274G2/face0413/val"
fs1 = os.listdir(path1)
# fs2 = os.listdir(path2)
for imagefolder in fs1:
print imagefolder
new_folder=os.path.join(dst_path,imagefolder)
print new_folder
if not os.path.exists(new_folder):
os.mkdir(new_folder)
path2 = os.path.join(path1, imagefolder)
fs2=os.listdir(path2)
# # for pic in imagefolder:
print len(fs2)
# print type(str(path2))
total_num=len(fs2)
threshold=int(total_num*0.5)
print threshold
num=0
for pic in fs2:
# print type(pic)
# print os.path.join(new_folder,pic)
if num<=threshold:
num+=1
shutil.move(os.path.join(path2,pic), os.path.join(new_folder, pic))
else:break
4.读取txt文件的每一行
import re
fopen0 = open("supplement/SeNet-0.005-64-50-1.txt", 'r')
# fopen0 = open("resnet-cfg/Config2rN.cfg", 'r')
lines0 = fopen0.readlines() #读取每一行到lines0
model0=[]
for line in lines0:
m0 = re.findall(r'(\w*[0-9]+)\w*', line) #找出每一行中的数字
# sheet.write(i, 0, m)
# i = i + 1
# print m0
if len(m0)>=3:
# tem0=m0[1]
# print tem0
# print m0[1]
model0.append(m0[1])
# model0.append(m0[2])
print model0
print len(model0