Python writelines() and write() huge time difference

后端 未结 3 668
攒了一身酷
攒了一身酷 2021-02-05 01:51

I was working on a script which reading a folder of files(each of size ranging from 20 MB to 100 MB), modifies some data in each line, and writes back to a copy of the file.

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 02:19

    as a complement to Martijn answer, the best way would be to avoid to build the list using join in the first place

    Just pass a generator comprehension to writelines, adding the newline in the end: no unnecessary memory allocation and no loop (besides the comprehension)

    myWrite.writelines("{}\n".format(x) for x in my_list)
    

提交回复
热议问题