[英]How to search if path exists in a directory recursively with python?
您好,我想知道路徑是否遞歸存在於目錄中。 我寫了一個腳本,但是太慢了。 所以我想知道還有其他方法。 'path'是完整路徑的部分字符串。
bMatch = False
for dirpath, dirnames, filenames in os.walk("."):
for f in filenames :
full_path = os.path.join(dirpath, f)
logger.error(full_path)
if path in full_path :
bMatch = True
logger.error("Matched")
break
if bMatch == True :
break
if bMatch == False :
logger.error("[Error] Path doesn't exist. Please check " + path + ' in the row ' + str(row + 1))
您可以使用os.listdir
並將op存儲到queue
然后可以驗證路徑。 請參閱我的玩具項目,以了解如何過濾隱藏和可見的目錄和文件。 您將獲得有關在那里處理文件和目錄的基本思想。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.