# -*- coding: utf-8 -*-
"""
Created on Sat Jul 29 08:52:28 2023
1. 函数介绍
1.1 已有路径列表, 筛选 folder | file | str
select_file, select_folder, classify_file_folder_else
注:以上函数: 输入list, 输出list
1.2 获取浅层路径列表
推荐函数(支持列表输入, 展开文件夹的浅层, 进行分类):
get_shallow_files, (输入list|str, 输出list)
get_shallow_folders, (输入list|str, 输出list)
get_shallow_file_folder_else(输入list|str, 输出dict)
其它函数:
get_shallow_paths(不分类, 输入:list|str, 输出list),
get_shallow_paths_by_str(输入:str, 输出list)
@author: zkding
"""
#%% import
import os
#%% com func - 路径列表 内容分拣
def select_file(pths):
"""in: 路径list, 元素str: 文件|文件夹|其它
out: 其中的file"""
ret = []
for pth in pths:
if os.path.isfile(pth):
ret += [pth]
return ret
def select_folder(pths):
"""in: 路径list, 元素str: 文件|文件夹|其它
out: 其中的folder"""
ret = []
for pth in pths:
if os.path.isdir(pth):
ret += [pth]
return ret
def classify_file_folder_else(pths):
"""in: 路径list, 元素str: 文件|文件夹|其它
out: {"file":[], "folder":[], "else":[]}
"""
ret = {"file":[], "folder":[], "else":[]}
for pth in pths:
if os.path.isfile(pth):
ret["file"] += [pth]
elif os.path.isdir(pth):
ret["folder"] += [pth]
else:
ret["else"] += [pth]
return ret
#%% com func - 浅层文件
def get_shallow_paths_by_str(pth, keep_unk=False):
"""in: 路径str 文件|文件夹|其它
out: 路径列表list (浅层)"""
if os.path.isdir(pth):
if pth[-1] not in "/\\":
pth += "/"
fnames = os.listdir(pth)
return [pth+fname for fname in fnames]
elif os.path.isfile(pth):
return [pth]
return [pth] if keep_unk else []
def get_shallow_paths(pth, keep_unk=False, list_recur=False):
"""
in:
类型1: 路径list :
元素类型1: str - 文件|文件夹|其它
元素类型2: list - 可选递归, 默认不递归(直接并入结果)
类型2: 路径str: 文件|文件夹|其它
out: 路径列表list (浅层)
"""
if not pth:
return []
if type(pth) == list:
ret = []
if list_recur:
# 进行 内部列表 递归
for p in pth:
ret += get_shallow_paths(p, keep_unk, list_recur)
return ret
# 不进行 内部列表 递归
for elm in pth:
if type(elm) == str:
ret += get_shallow_paths_by_str(elm, keep_unk)
elif type(elm) == list:
ret += elm
return ret
elif type(pth) == str:
return get_shallow_paths_by_str(pth, keep_unk)
return []
def get_shallow_files(pth, list_recur=False):
"""
in:
类型1: 路径list :
元素类型1: str - 文件|文件夹|其它
元素类型2: list - 可选递归, 默认不递归(直接并入结果)
类型2: 路径str: 文件|文件夹|其它
out:
浅层的 file
"""
return select_file(
get_shallow_paths(pth, list_recur)
)
def get_shallow_folders(pth, list_recur=False):
"""
in:
类型1: 路径list :
元素类型1: str - 文件|文件夹|其它
元素类型2: list - 可选递归, 默认不递归(直接并入结果)
类型2: 路径str: 文件|文件夹|其它
out:
浅层的 folder
"""
return select_folder(
get_shallow_paths(pth, list_recur)
)
def get_shallow_file_folder_else(pth, list_recur=False):
"""
in:
类型1: 路径list :
元素类型1: str - 文件|文件夹|其它
元素类型2: list - 可选递归, 默认不递归(直接并入结果)
类型2: 路径str: 文件|文件夹|其它
out:
浅层的 {"file":[], "folder":[], "else":[]}
"""
return classify_file_folder_else(
get_shallow_paths(pth, keep_unk=True, list_recur=list_recur)
)
#%% main
if __name__ == "__main__":
print("start testing ...")
# get files
pth_list = [
"./", # 文件夹
r"../output\0_url_protocal_json", # 文件夹
r"../output\1_base_train_data/story_plugin_0", # 文件, 获取浅层
"abcd", # 普通str,
[ r"../", "1234" ] # 子list, 可选递归 | 简单并入(默认)
]
print("\nfile folder else:\n",get_shallow_file_folder_else(pth_list))# 子list 简单并入
print("\n\nfile folder else (子list 递归):\n",get_shallow_file_folder_else(pth_list, True)) # 子list 递归
py基础工具-路径:浅层路径 获取or分拣
最新推荐文章于 2024-05-15 22:12:36 发布