python讀取多層嵌套文件夾中的文件(zip文件嵌套在不同層級的文件夾中)


python讀取多層嵌套文件夾中的文件(zip文件嵌套在不同層級的文件夾中):

使用遞歸函數,使用python的os.path.isfile方法判斷當前是不是.zip文件,如果不是再用os.listdir方法將子目錄循環遞歸判斷。

import os
path = 'D:\關鍵\底圖\vec_w'
path_read = []  #path_read saves all executable files

def check_if_dir(file_path):
temp_list = os.listdir(file_path)  #put file name from file_path in temp_list
for temp_list_each in temp_list:

pathName=file_path + '/' + temp_list_each
if os.path.isfile(pathName):
if os.path.splitext(pathName)[-1] == '.zip': #自己需要處理的是.zip文件所以在此加一個判斷
path_read.append(pathName)
else:
continue
else:
check_if_dir(pathName) #loop traversal

check_if_dir(path)
#print(len(path_read))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM