Does the Python “open” function save its content in memory or in a temp file?

后端 未结 6 1533
孤街浪徒
孤街浪徒 2021-01-18 01:42

For the following Python code:

fp = open(\'output.txt\', \'wb\')
# Very big file, writes a lot of lines, n is a very large number
for i in range(1, n):
    f         


        
6条回答
  •  执笔经年
    2021-01-18 02:00

    If you a writing out a large file for which the writes might fail you a better off flushing the file to disk yourself at regular intervals using fp.flush(). This way the file will be in a location of your choosing that you can easily get to rather than being at the mercy of the OS:

    fp = open('output.txt', 'wb')
    counter = 0
    for line in many_lines:
        file.write(line)
        counter += 1
        if counter > 999:
            fp.flush()
    fp.close()
    

    This will flush the file to disk every 1000 lines.

提交回复
热议问题