[item for item in os.listdir('/') if os.path.isdir(item)]
回傳是空,這肯定是不對的,根目錄下明明有很多目錄的,
原來os.path.isdir檢查的目錄是在當前目錄下的,而不是os.listdir清單('/')下的目錄下,我的目錄并沒有切到/所以回傳為空,
正確的做法是使用join
[item for item in os.listdir('/') if os.path.isdir(os.path.join('/',item))]
參考:https://www.pythonheidong.com/blog/article/289665/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/142953.html
標籤:Python
上一篇:Python基礎-06字典
