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

后端 未结 6 1528
孤街浪徒
孤街浪徒 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:09

    Building on ataylor's comment to the question:

    You might want to nest your loop. Something like

    for i in range(1,n):
        for each in range n:
            fp.write('something')
    fp.close()
    

    That way, the only thing that gets put into memory is the string "something", not "something" * n.

提交回复
热议问题