1. 程式人生 > 其它 >python3_列出路徑下的資料夾/檔案

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'>