我正在编写一个python脚本,我在树目录中搜索特定的字符串。我要求最终用户定义他们希望在搜索中包含哪些文件夹,并且一旦脚本找到用户想要扫描的文件夹之一,该脚本应该也扫描所有子文件夹选定的文件夹。
我正在尝试做几个for循环,但我无法让它工作。
脚本的开头如下:
startTime = datetime.datetime.now()
option = input("Do you want to scan: A) Excel B) PDF C) Both Q) Quit: ")
option = option.lower()
if (option == "b") or (option == "b") or (option == "c"):
stringToSearch = input("Which string do you want to search? ")
folderToSearch = input("Which top folder to search from(i.e. Z:\S 15\BOMs)? ")
subfoldersToSearch = input("Which subfolders(i.e. BOMs, Catalogs? <NO ANSWER = ALL) ")
print("Press CTRL + C to stop the search")
for foldername, subfolders, filenames in os.walk(folderToSearch):
for filename in filenames:
if (subfoldersToSearch == "") or (subfoldersToSearch in foldername):
print(subfoldersToSearch, "+++", foldername)
for x_foldername, x_subfolders, x_filenames in os.walk(foldername):
totalFiles += 1
for x_filename in x_filenames:
if (x_filename.endswith('.pdf') and option == "b") or (x_filename.endswith('.pdf') and option == "c"):
[剩下的东西]
问题是它进入一个连续的循环,因为只要它完成了一个选定的文件夹,它就会回到第一个for循环,并尝试再次走同一个选定的文件夹。
有没有更好的方法来执行此操作os.walk
?
基本上,我希望脚本找到一个特定的文件夹,然后扫描该文件夹的内容(包括文件夹),然后继续前往下一个文件夹而不重新开始。
答案 0 :(得分:0)
我想通了,它实际上只适用于一个for循环。以下是新代码的外观,希望它能帮助将来的某个人......最好的
startTime = datetime.datetime.now()
option = input("Do you want to scan: A) Excel B) PDF C) Both Q) Quit: ")
option = option.lower()
if (option == "b") or (option == "b") or (option == "c"):
stringToSearch = input("Which string do you want to search? ")
folderToSearch = input("Which top folder to search from(i.e. Z:\S 15\BOMs)? ")
subfoldersToSearch = input("Which subfolders(i.e. BOMs, Catalogs? <NO ANSWER = ALL) ")
print("Press CTRL + C to stop the search")
for foldername, subfolders, filenames in os.walk(folderToSearch, topdown=True):
print(subfolders)
for filename in filenames:
if (subfoldersToSearch == "") or (subfoldersToSearch in foldername):
print(subfoldersToSearch, "+++", foldername)