Python脚本需要很长时间才能执行

2024-10-02 12:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太擅长python。我下面的脚本需要很长的时间来执行分裂。下面的代码中有什么我需要更改的吗?我的猜测是,缓慢的行为是由于“for循环”。目前我使用的是python版本2.7.8

有人能帮忙吗

“soapUIResponseFolder”包含格式为“Responses\u 20121115\u TestSuite\u 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

Tags: thepathinforosfilesfilenameglob
1条回答
网友
1楼 · 发布于 2024-10-02 12:28:57

文件夹扫描速度

请去掉python代码,只查找和打印相关文件,并将“speed”与find somewhere -type f进行比较。如果您的python代码明显较慢,请考虑使用scandir(第三方)或包含它的python3.5。你知道吗

使用glob

glob.glob(pattern)接受类似*.csv的shell样式模式,在您的情况下,您可以使用os.listdir()

相关问题 更多 >

    热门问题