ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python3_列出路径下的文件夹/文件

2022-05-27 13:33:21  阅读:218  来源: 互联网

标签:files mbn path1 cur 文件夹 file path 列出 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'>

 

标签:files,mbn,path1,cur,文件夹,file,path,列出,python3
来源: https://www.cnblogs.com/bluebluehi/p/16317249.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有