【python】文件操作(2)
程序员文章站
2022-03-27 21:05:11
# -*- coding: utf-8 -*-"""file_utils: 文件操作@author: libo"""import osdef get_files(img_dir): imgs, masks, xmls = list_files(img_dir) return imgs, masks, xmlsdef list_files(in_path): """ 将文件夹中的各种文件分割开来 """ img_files = [] mask_file...
# -*- coding: utf-8 -*-
"""
file_utils: 文件操作
@author: libo
"""
import os
def get_files(img_dir):
imgs, masks, xmls = list_files(img_dir)
return imgs, masks, xmls
def list_files(in_path):
""" 将文件夹中的各种文件分割开来 """
img_files = []
mask_files = []
gt_files = []
for (dirpath, dirnames, filenames) in os.walk(in_path):
for file in filenames:
filename, ext = os.path.splitext(file) # 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
ext = str.lower(ext)
if ext == '.jpg' or ext == '.jpeg' or ext == '.gif' or ext == '.png' or ext == '.pgm':
img_files.append(os.path.join(dirpath, file))
elif ext == '.bmp':
mask_files.append(os.path.join(dirpath, file))
elif ext == '.xml' or ext == '.gt' or ext == '.txt':
gt_files.append(os.path.join(dirpath, file))
elif ext == '.zip':
continue
return img_files, mask_files, gt_files
本文地址:https://blog.csdn.net/libo1004/article/details/110920382
上一篇: 计算机网络基础——通信基础