简体   繁体   中英

Improve performance of Writing a file - Python 3.4

I am not well versed with Python, based on my knowledge and some browsing I wrote the script mentioned below, this script basically looks for all files in C:\\temp\\dats folder and writes it in C:\\temp\\datsOutput\\output.text file, for some reason my code is running terribly slow, can anyone advise me to improve it to have a better performance?

    import os
    a = open(r"C:\temp\datsOutput\output.txt", "w")
    path = r'C:\temp\dats'
    for filename in os.listdir(path):
        fullPath = path+"\\"+filename
        with open(fullPath, "r") as ins:
                for line in ins:
                    a.write(line)

Two speedups. First, copy the whole file at once. Second, treat the files as binary (add a “b” after the “r” or “w” when opening a file.)

Combined, runs about 10x faster.

Final code looks like this

import os
a = open(r"C:\temp\datsOutput\output.txt", "wb")
path = r'C:\temp\dats'
for filename in os.listdir(path):
    fullPath = path+"\\"+filename
    with open(fullPath, "rb") as ins:
            a.write(ins.read())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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