看板 Python 關於我們 聯絡資訊
我想要檢查資料夾底下有無*.txt的檔案 import os fileName='*.txt' if os.path.exists(fileName): print ("YES") else : print ("NO") 如果*.txt改成test.txt話便可以顯示YES 但是我用*.txt,卻顯示NO 難道我*的用法有錯誤嗎? 目前我資料夾底下只有一個test.txt 未來如果有test.txt test1.txt 這樣的做法也行嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 42.67.135.24
grapherd:import glob; glob.glob("*.txt") 01/09 16:07
blackspace98:謝謝~不過我可以知道為什麼嗎?再次謝嘍!! 01/09 16:32
grapherd:根據Document *1, Python對路徑位置不使用萬用字元, 01/09 18:16
grapherd:但是 glob 模組調用 fnmatch模組, 所以可以使用萬用字元 01/09 18:17
grapherd:*1: http://ppt.cc/FNIl, glob: http://ppt.cc/4VJ_ 01/09 18:19
grapherd:fnmatch: http://ppt.cc/fkEt 01/09 18:20
blackspace98:謝謝您~非常感激呀!! 01/09 18:51