我编写了一个程序来查找大小大于等于100Mb的大文件,
但是,它在MacOS上无休止地运行
我设置了
sentinel = True
while sentinel:
和破坏条件:
sentinel = False
完整代码:
import os, time, shelve, logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
logging.info("Start of Program")
start = time.time()
root = '/'
# errors= set()
# dirs = set()
sentinel = True
while sentinel:
try:
root = os.path.abspath(root) #ensure its a abspath
#set the baseline as 100M
#consider the shift
baseline = 100 * 2**20 # 2*20 is 1M
#setup to collect the large files
large_files = []
#root is a better choise as the a concept
for foldername, subfolders, files in os.walk(root):
# logging.error("foldername: %s" %foldername)
# print("subfolders: ", subfolders)
for f in files:
# print(f"{foldername}, {f}")
abspath = os.path.join(foldername, f)
logging.debug("abspath: %s" %abspath)
size = os.path.getsize(abspath)
if size >= baseline:
large_files.append((os.path.basename(abspath), size/2**20))
# turn_end = time.time()
# print(f"UnitTimer: {turn_end-start}") #no spaces beween .
#write the large files to shelf
logging.debug("subfolders: " + str(subfolders))
shelf = shelve.open('large_files')
shelf.clear()
shelf["large_files"] = large_files
shelf.close()
end = time.time()
logging.debug("Timer: %s." %(end-start))
#break the while loop
logging.info("End of Program")
#break the loop after walk()
sentinel = False
except (PermissionError,FileNotFoundError) as e:
# errors.add(e)
pass
代码无休止,但我找不到问题。
答案 0 :(得分:2)
仅在没有例外的情况下将前哨设置为False
。还要确保在您的False
块中将其设置为except
。
要从错误中正常恢复,您可能不想将每个文件访问都包装在同一try / except块中。相反,您希望有一个小的try / except块来捕获单个文件操作,如果失败,则可以应用错误处理代码(例如重试或记录并继续到下一个文件)。