python遍历txt文件并统计_python遍历整个文件夹中的所有.txt文件并将绝对路径保存在指定的txt文档中...

在深度学习处理数据时经常会用到的一步操作,路径名因人而异,可以根据需求自行更改。

import os

# get .txt document

rootdir=os.path.join('要读取的txt文件的绝对路径')

# read

write_path=open('要写入的txt文件的绝对路径/write.txt','w')

for (dirpath,dirnames,filenames) in os.walk(rootdir):

for filename in filenames:

if os.path.splitext(filename)[1]=='.txt':

write_path.write('要写的绝对路径'+filename+'\n')

write_path.close()

import os

import cv2

from tqdm import tqdm

def main(source_root):

cwd = os.getcwd() # delete '.DS_Store' existed in the source_root

os.chdir(source_root)

os.system("find . -name '*.DS_Store' -type f -delete")

os.chdir(cwd)

write_path=open('./data/refined_casia.txt','w')

for subfolder in tqdm(os.listdir(source_root)):

for image_name in os.listdir(os.path.join(source_root, subfolder)):

print("Processing\t{}".format(os.path.join(source_root, subfolder, image_name)))

img = cv2.imread(os.path.join(source_root, subfolder, image_name))

if type(img) == type(None):

print("damaged image %s, del it" % (img))

os.remove(img)

continue

write_path.write(os.path.join(source_root, subfolder, image_name.split('.')[0] + '.jpg' + '\n'))

write_path.close()

if __name__ == "__main__":

main(source_root = "./data/imgs")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值