我有以下目錄結構
$ find
.
./file1.html
./soquest_glob.py
./dir1
./dir1/file2.html
./dir2
./dir2/file3.html
(我在上面添加了空白行以闡明不同檔案夾中的檔案)。
我正在嘗試使用glob包使用以下 Python 代碼查找所有 html 檔案(包括子檔案夾中的檔案):
$ cat soquest_glob.py
import glob
flist = glob.glob("*.html", recursive=True)
print(flist)
但是,當我運行此代碼時,它只在當前檔案夾中找到檔案,而不是在子檔案夾中:
$ python3 soquest_glob.py
['file1.html']
問題出在哪里,如何解決?
uj5u.com熱心網友回復:
影響 的行為的recursive論點。您需要使用如下模式:.glob.glob**glob.glob("**/*.html", recursive=True)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520833.html
標籤:Python文件目录
上一篇:多次寫入的PHP檔案鎖定問題
