Python 打印目錄和目錄下所有文件


#!/usr/bin/env python3
#!-*- coding:UTF-8 -*-

import os
class GetPath():# 獲取所有目錄
@staticmethod
def get_dir(path):
print("打印所有目錄:")
for root, dirs, files in os.walk(path):
for dir in dirs:
print(os.path.join(root,dir))

@staticmethod
def get_file(path): # 獲取文件路徑
print("所有文件路徑是:")
for root, dirs, files in os.walk(path):
for file in files:
print(os.path.join(root,file))

if __name__ == '__main__':
path="/opt"
GetPath.get_dir(path)
GetPath.get_file(path)


免責聲明!

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



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