Improve performance of Writing a file - Python 3.4 -
i not versed python, based on knowledge , browsing wrote script mentioned below, script looks files in c:\temp\dats folder , writes in c:\temp\datsoutput\output.text file, reason code running terribly slow, can advise me improve have better performance?
import os = open(r"c:\temp\datsoutput\output.txt", "w") path = r'c:\temp\dats' filename in os.listdir(path): fullpath = path+"\\"+filename open(fullpath, "r") ins: line in ins: a.write(line)
two speedups. first, copy whole file @ once. second, treat files binary (add “b” after “r” or “w” when opening file.)
combined, runs 10x faster.
final code looks this
import os = open(r"c:\temp\datsoutput\output.txt", "wb") path = r'c:\temp\dats' filename in os.listdir(path): fullpath = path+"\\"+filename open(fullpath, "rb") ins: a.write(ins.read())
Comments
Post a Comment