简体   繁体   English

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

[英]Python script takes long to execute

I am not that much of an expert in python. 我不是python的专家。 My below script takes very long to execute during the split. 我的以下脚本在拆分期间需要很长时间执行。 Is there anything I need to change in the below code? 我需要在下面的代码中进行任何更改吗? My guess is that the slow behavior is due to the the "for-loop". 我的猜测是,行为缓慢是由于“ for-loop”造成的。 Currently I am using python version 2.7.8 当前我正在使用python版本2.7.8

anybody please help on this 有人请帮忙

" soapUIResponseFolder" contains folder in the format of "Responses_20121115_TestSuite_1" “ 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

Folder scan speed 文件夹扫描速度

Please strip down your python code to only find and print out relevant files and compare "speed" with find somewhere -type f . 请精简您的python代码,仅查找并打印出相关文件,并将“速度”与find somewhere -type f If your python code is notably slower, consider using scandir (3rd party) or Python 3.5 which includes it. 如果您的Python代码明显较慢,请考虑使用scandir (第三方)或包含它的Python 3.5。

Using glob 使用glob

glob.glob(pattern) accepts a shell-style pattern like *.csv , in your case you could have as easily used os.listdir() glob.glob(pattern)接受*.csv类的shell样式的模式,在您的情况下,您可以像使用os.listdir()一样容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM