Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writting multiple times in a file in a loop using python

Tags:

python

I'm writing large matrices in python, like (2^20, 2^20). To deal with that my approach is to write each element in a file with its respective row and column.

I've tried to resolve it in like that:

l = 20
j = 1
delt = -1
for x in range(0,2**l):
    for y in range(0,l):
        k = (y+1)%l
        if check_nth_bit(x,y) == 0:
            a = ([x,x,-j*h/2])
            with open("file.txt", "w") as f:
                f.write(str(a))
        else:
            b = ([x,x,j*h/2])
            with open("file.txt", "w") as f:
                f.write(str(b))

The way I've done, only the last element is written in the file. Can anyone help me?

like image 551
lzcostademoraes Avatar asked Nov 30 '25 07:11

lzcostademoraes


1 Answers

Each time you use with open("file.txt", "w") as f: you're opening the file in write mode - specifically, in overwrite mode. Every value is being written to the file, but each time you loop, you erase the file and start writing it anew.

You can avoid this by opening in append mode as with open("file.txt", "a") as f: but opening and closing files over and over doesn't make much sense (and it's computationally very expensive; your program will get very slow!). Why not move that logic outside of your loops, so you only have to open the file once?

l = 20
j = 1
delt = -1
with open ("file.txt", "w") as f:
    for x in range(0,2**l):
        for y in range(0,l):
            k = (y+1)%l
            if check_nth_bit(x,y) == 0:
                a = ([x,x,-j*h/2])-
                f.write(str(a))
            else:
                b = ([x,x,j*h/2])-
                f.write(str(b))
like image 157
Nick Reed Avatar answered Dec 02 '25 19:12

Nick Reed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!