Python os模組的walk函式

2021-08-28 21:48:32 字數 333 閱讀 5737

walk函式返回三引數,分別是當前資料夾名稱,當前資料夾的子資料夾,當前資料夾裡的檔案

所以通過乙個for迴圈可以遍歷整個資料夾裡的內容

import os

for curfile,subfolders,filenames in os.walk(os.getcwd()+"/xx"):

print(curfile)

for subfolder in subfolders:

print(subfolder)

for filename in filenames:

print(filename)

python os介紹 Python os模組介紹

os模組主要用於執行系統命令 import os os.remname file.txt file1.txt 檔案重新命名 os.remove file1.txt 刪除檔案 os.mkdir test 建立資料夾 os.rmdir test 刪除資料夾 os.sep 可以取代作業系統特定的路徑分割符...

python os模組的應用

舉例中的目錄形式如下所示 in 36 pwd out 36 home python desktop code in 37 ls hello.py hello.txt test.py 資料夾01 資料夾02 資料夾03 1.當前路徑及路徑下的檔案 os.getcwd 檢視當前所在路徑。os.listd...

Python os模組的運用

import os 獲得當前操作的絕對路徑 abspath os.path.abspath real.txt print abspath 檢視當前目錄 curpath os.curdir print curpath 獲得當前工作目錄 cwd os.getcwd print cwd 獲得該目錄下所有的...