我正在编写一个脚本来浏览文件并打印出zip文件及其创建日期。我的程序适用于少于5 MB的数据,但是如果我在超过15 MB的数据上运行它,则生成输出需要数小时。以下是我的代码示例。如何优化我的代码以便它可以更快地运行?
import os, argparse
from datetime import datetime
#Returns a list of directories of all zip files.
def findZip_Dir_list(cwd):
userList = []
for (dirname, dirs, files) in os.walk(cwd):
for filename in files:
if filename.endswith('.zip'):
fileDir = os.path.join(dirname, filename)
t = os.path.getmtime(fileDir)
userList.append(os.path.join(fileDir, str(t)))
return userList
cwd = os.getcwd()
testList = findZip_Dir_list(cwd)
print(tesList)
答案 0 :(得分:0)
使用glob2
import os, glob2
# Returns a list of directories of all zip files.
def findZip_Dir_list(cwd):
userList = []
all_zip_files = glob2.glob(os.path.join(cwd, '**/*.zip'))
for file in all_zip_files:
t = os.path.getmtime(file)
userList.append(os.path.join(file, str(t)))
return userList
cwd = os.getcwd()
testList = findZip_Dir_list(cwd)
print(testList)