如何使用 Python 遞迴地遍歷所有檔案?
要遞迴遍歷所有檔案,你需要使用 os.walk 遍歷目錄樹,並使用 os.utime(path_to_file) 遍歷其中的所有檔案。
示例
import os # Recursively walk the tree for root, dirs, files in os.walk(path): for file in files: # Set utime to current time os.utime(os.path.join(root, file))
在 Python 3.4+ 中,你可以直接使用 pathlib 模組來遍歷檔案。
示例
from pathlib import Path import os # Recursively walk the tree for root, dirs, files in os.walk(path): for file in files: Path(os.path.join(root, file)).touch()
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP