我对python的专家不是那么多。我的下面的脚本在拆分期间需要很长时间才能执行。在下面的代码中有什么我需要改变的吗?我的猜测是,缓慢的行为是由于" for-loop"。目前我正在使用python版本2.7.8
任何人请帮忙解决这个问题
" soapUIResponseFolder"包含格式为" Responses_20121115_TestSuite_1"
for subdir in glob.glob(soapUIResponseFolder):
subdir = os.path.normpath(subdir)
# This is a case when the os.walk returns a tuple (sequences). So we need to pick only the files path.
onlyfiles = reduce(lambda x,y : x+y, [map(lambda x: root + "/" + x, files) for root, dirs, files in os.walk(subdir)])
for file in onlyfiles:
i = 0
file = os.path.normpath(file)
fileNameWithExt = os.path.basename(file)
filename, fileExtension = os.path.splitext(fileNameWithExt)
fileExtension = ".xml"
# The filename contains several parts with - as delimiter. So lets pick only the relevant ones
part1,part2,part3,part4,part5=filename.split("-")
filename = part2+"-"+part3 + fileExtension
responseFolder = os.path.split(os.path.dirname(file))[1]
inputFolder_new = inputFolder+"/"+responseFolder
outputFolder_new = outputFolder+"/"+responseFolder
答案 0 :(得分:0)
文件夹扫描速度
请删除您的python代码,仅查找并打印相关文件,并将“速度”与find somewhere -type f
进行比较。如果您的python代码明显变慢,请考虑使用scandir
(第三方)或包含它的Python 3.5。
使用glob
glob.glob(pattern)
接受像*.csv
这样的shell样式模式,在您的情况下,您可以轻松使用os.listdir()