python3_列出路径下的文件夹/文件
(15条消息) python只列出当前目录下的文件夹(排除文件)_fjh1997的博客-CSDN博客_python 只列出文件夹
1、列出文件夹
from pathlib import Path
def ListDir(path): path = Path(path) dirs = [e for e in path.iterdir() if e.is_dir()] for dir in dirs: # dir = str(dir).split(path)[1] print(dir) ListDir(path) ## E:\AUTO_test\9628\backpack\AG35CEVAR08A09V02T4G_OCPU_NEUE_P4\dbg E:\AUTO_test\9628\backpack\AG35CEVAR08A09V02T4G_OCPU_NEUE_P4\modem_bin_orig E:\AUTO_test\9628\backpack\AG35CEVAR08A09V02T4G_OCPU_NEUE_P4\update E:\AUTO_test\9628\backpack\AG35CEVAR08A09V02T4G_OCPU_NEUE_P4\upgrade
2、列出文件
(15条消息) Python3功能篇一:如何搜索某个文件夹里的所有文件(包括其子文件夹里的文件)?_天亮继续睡的博客-CSDN博客_python查找文件夹下的文件
(15条消息) python列出文件夹下所有文件的四个方法_poem_of_sunshine的博客-CSDN博客_python 列出所有目录
Python os.path模块常见函数用法(实例+详细注释) (biancheng.net)
# 导入需要的库 import os # 子函数,显示所有文件的路径 def show_files(path, all_files): # 显示当前目录所有文件和子文件夹,放入file_list数组里 file_list = os.listdir(path) # 循环判断每个file_list里的元素是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归 for file in file_list: # 利用os.path.join()方法取得路径全名,并存入cur_path变量 cur_path = os.path.join(path, file) # 判断是否是文件夹 if os.path.isdir(cur_path): # 递归 show_files(cur_path, all_files) else: # 将file添加进all_files里 all_files.append(file) return all_files # 以下是主程序 # 传入空的list接收文件名 contents = show_files("这里放目标文件夹的路径", []) # 循环打印show_files函数返回的文件名列表 for content in contents: print(content)
3、列出路径下不包含某个文件夹的文件列表
from pathlib import Path import os def show_files(path, all_files,check_path1,check_path2,check_path3): global fac_files global no_fac_files # 显示当前目录所有文件和子文件夹,放入file_list数组里 file_list = os.listdir(path) # 循环判断每个file_list里的元素是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归 for file in file_list: # 利用os.path.join()方法取得路径全名,并存入cur_path变量 cur_path = os.path.join(path, file) # print('111cur_path:-{}-{}'.format(cur_path,type(cur_path))) if check_path1 in cur_path or check_path2 in cur_path or check_path3 in cur_path: continue else: # 判断是否是文件夹 if os.path.isdir(cur_path): # 递归 # print('222os.path.isdir(cur_path):-{}-{}'.format(os.path.isdir(cur_path),type(os.path.isdir(cur_path)))) show_files(cur_path, all_files,check_path1,check_path2,check_path3) else: # 将file添加进all_files里 all_files.append(file) # print(all_files) return all_files path1 = r'E:\AUTO_test\9628\backpack\AG35CEVAR08A08V01T4G_OCPU_NEUE_P4_factory' path2 = r'E:\AUTO_test\9628\backpack\AG35CEVAR08A09V02T4G_OCPU_NEUE_P4' dbg_path1 = path1 + '\dbg' dbg_path2 = path2 + '\dbg' upgrade_path1 = path1 + r'\upgrade' upgrade_path2 = path2 + r'\upgrade' modem_bin_orig_path1 = path1 + r'\modem_bin_orig' modem_bin_orig_path2 = path2 + r'\modem_bin_orig' all_files = [] res1 = show_files(path1, all_files,dbg_path1,upgrade_path1,modem_bin_orig_path1) print(res1) all_files = [] res2 = show_files(path2, all_files,dbg_path2,upgrade_path1,modem_bin_orig_path2) print(res2) diff1 = set(res1).difference(set(res2)) print(diff1,type(diff1)) diff2 = set(res2).difference(set(res1)) print(diff2,type(diff2)) ###C:\Users\jodie.xu\AppData\Local\Programs\Python\Python36\python.exe E:/AUTO_test/9628/path.py ['contents.xml', 'md5.txt', 'appsboot.mbn', 'cefs.mbn', 'devcfg.mbn', 'ENPRG9x07.mbn', 'factory.xqcn', 'partition_complete_p2K_b128K.mbn', 'patch_p2K_b128K.xml', 'prog_nand_firehose_9x07.mbn', 'rawprogram_nand_p2K_b128K_factory.xml', 'mdm9607-boot.img', 'mdm9607-sysfs.ubi', 'NON-HLOS.ubi', 'NPRG9x07.mbn', 'oemapp.ubi', 'oemrw.ubi', 'partition.mbn', 'partition_nand.xml', 'persist.ubi', 'quectelrw.ubi', 'rpm.mbn', 'sbl1.mbn', 'tz.mbn'] ['contents.xml', 'md5.txt', 'appsboot.mbn', 'devcfg.mbn', 'ENPRG9x07.mbn', 'partition_complete_p2K_b128K.mbn', 'patch_p2K_b128K.xml', 'prog_nand_firehose_9x07.mbn', 'rawprogram_nand_p2K_b128K_update.xml', 'mdm9607-boot.img', 'mdm9607-sysfs.ubi', 'NON-HLOS.ubi', 'NPRG9x07.mbn', 'oemapp.ubi', 'oemrw.ubi', 'partition.mbn', 'partition_nand.xml', 'persist.ubi', 'quectelrw.ubi', 'rpm.mbn', 'sbl1.mbn', 'tz.mbn'] {'cefs.mbn', 'factory.xqcn', 'rawprogram_nand_p2K_b128K_factory.xml'} <class 'set'> {'rawprogram_nand_p2K_b128K_update.xml'} <class 'set'>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术