Ron*_*ohn 6 python python-2.7 python-3.x
在我重新发明轮子之前,谁能告诉我是否有单行语句的直接(或半直接)替代:
allfiles = dircache.listdir('.')
Run Code Online (Sandbox Code Playgroud)
一条线?不,但你可以这样做:
global_cache = {}
def cached_listdir(path):
res = global_cache.get(path)
if res is None:
res = os.listdir(path)
global_cache[path] = res
return res
Run Code Online (Sandbox Code Playgroud)