Python Forum
file.write not working properly - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: file.write not working properly (/thread-22346.html)

Pages: 1 2


file.write not working properly - mnh001 - Nov-09-2019

I'm having a problem with writing to file. It only works part time. I have a print statement right after the file.write line and it shows that everything is being calculated correctly. Other scripts I've written don't have any problems. And I don't get any error messages. And yes, I already know the code isn't very pretty. I'm still learning this stuff.

Here's the code.

import numpy as np

#

def mat(num_in, side):
    rows = []
    A = []
    
    for y in range(0, side):
        for x in range(0, side):
            rows.append(int(num_in[x]))
        A.append(rows)
        rows = []
        rgt = num_in[side - 1:]
        temp = rgt
        for z in range(0, side - 1):
            temp = temp + num_in[z]
        num_in = str(temp)
    return A


side = 4; side1 = side
num_in = str(10**(side1 - 1))
hold = int(num_in)

fhand = open("cd4.txt", "w")

s = 0
chain = []
while int(hold) < 10**side1:
    while s == 0:
        A = mat(num_in, side)
        det = int(round(np.linalg.det(A)))
        if (det in chain or abs(det) in chain) or (det < 10 and det > -10):
            chain.append(det)
            break
        else:
            chain.append(det)
            num_in = str(abs(det))
            side = len(num_in)
    hold += 1
    num_in = str(hold)
    side = len(num_in)
    fhand.write(f'{hold - 1}, {chain}\n')
    print(f'{hold - 1}, {chain}')
    chain = []

fhand.close
If the the range is say from 100 to 1000 it only prints up to 720. Or range is 1000 to 10000 it stops at 9884. And if the range is small say 10 to 20 it doesn't write to file at all. What's up with the selective write? Is it tired or something? Huh TIA


RE: file.write not working properly - Gribouillis - Nov-09-2019

mnh001 Wrote:If the the range is say from 100 to 1000 it only prints up to 720.
Which range are you talking about? Because I'm running this code and I don't have any problem.

Also note that if there is an issue and you wonder whether the error is in your code or in the builtin functions, then the error is in your code.


RE: file.write not working properly - mnh001 - Nov-09-2019

Oh ya, I know it's my code. I never assume I'm smarter than the code.
Anyway, all 3 ranges I've tried so far(10-100 no write at all, 100-1000 only writes up to 720, 1000-10000 only writes to 9884) fail to finish the job. It's frustrating since it only writes part. Like I said the PRINT statement to screen prints everything correctly. It's just the write to file that only goes part way. That I just don't get.


RE: file.write not working properly - Gribouillis - Nov-09-2019

I still don't understand where in the code is the range that you are talking about. Can you post the exact code that gives an error.


RE: file.write not working properly - mnh001 - Nov-09-2019

Oh, sorry. The range is the while loop in line 30.

If the side is 4 (line 22) then the code loops from 1000 to 9999 via line 30. The hold variable is incremented in line 41, which updates the num_in variable in line 42, which then goes and does it's thing.

I don't get ANY error messages.

In that range (in this case where side=4, from 1000 to 10000) it prints to screen (line 45) all the entries from 1000 to 9999 just like it's supposed to.

But the previous line (line 44) which prints the exact same info to file stops printing to file once it reaches 9884 instead of continuing all the way to 9999. (Oh, just for info sake, the function (mat) is computing the determinant of square matrices.)

And that same fhand.write in line 44, if the side is 2 (meaning it's doing 2x2 matrices from 10 to 99) it doesn't write ANYTHING to file.


RE: file.write not working properly - Gribouillis - Nov-09-2019

The problem is that I'm running the code and the last lines of cd4.txt are
Output:
9992, [9947, -2523, -192, 684, 216, 189, 1026, -999, 0] 9993, [6480, 3600, -1215, -405, 189, 1026, -999, 0] 9994, [3875, -1725, -675, 54, 9] 9995, [2048, -1904, -4368, -609, 945, 378, 378] 9996, [891, 1026, -999, 0] 9997, [272, 275, 266, 224, 32, 5] 9998, [35, -16, -35] 9999, [0]



RE: file.write not working properly - mnh001 - Nov-09-2019

Hmmm. For you it seems to be fine. For me, my cd4.txt ends like this:

Output:
9878, [0] 9879, [-165, 252, 81, 63, 27, -45, -9] 9880, [14625, 5598, 675, 54, 9] 9881, [10400, 1025, -416, 209, 737, 272, 275, 266, 224, 32, 5] 9882, [6993, 3645, -144, 81, 63, 27, -45, -9] 9883, [4368, -609, 945, 378, 378] 9884, [2465, -289, 817, 688, 88, 0]
I just can't figure out why. And if I use smaller ranges it won't go to file at all. The code obviously works as evidenced by your output. So why doesn't it here? Aggravating. Confused


RE: file.write not working properly - Gribouillis - Nov-09-2019

I see something! At the end of the script you forgot to close the file. Use fhand.close() instead of fhand.close. It may perhaps change something.


RE: file.write not working properly - mnh001 - Nov-09-2019

Ah, that did it. I totally forgot the parenthesis. Thanks.


RE: file.write not working properly - newbieAuggie2019 - Nov-09-2019

(Nov-09-2019, 07:59 PM)Gribouillis Wrote: I see something! At the end of the script you forgot to close the file. Use fhand.close() instead of fhand.close. It may perhaps change something.
Hi!

I was running the program and having the same result as mnh001, and when I changed the code as you suggested, the last lines of the txt file are then:
Output:
3864, [-1575, -1296, 2880, 3600, -1215, -405, 189, 1026, -999, 0] 3865, [-1584, 0] 3866, [-1495, 1235, -429, 585, 162, 189, 1026, -999, 0] 3867, [-1440, 225, 81, 63, 27, -45, -9] 3868, [-1575, -1296, 2880, 3600, -1215, -405, 189, 1026, -999, 0] 3869, [-2080, 3600, -1215, -405, 189, 1026, -999, 0]
(Nov-09-2019, 08:09 PM)mnh001 Wrote: Ah, that did it. I totally forgot the parenthesis. Thanks.

I was having the same result as you on the txt file, but to have the same results in both the txt file and on screen, I had to do the change suggested by Gribouillis, but I also had to interchange the order of lines 44 and 45 (I don't know why or how, though):

import numpy as np
 
#
 
def mat(num_in, side):
    rows = []
    A = []
        
    for y in range(0, side):
        for x in range(0, side):
            rows.append(int(num_in[x]))
        A.append(rows)
        rows = []
        rgt = num_in[side - 1:]
        temp = rgt
        for z in range(0, side - 1):
            temp = temp + num_in[z]
        num_in = str(temp)
    return A
 
 
side = 4; side1 = side
num_in = str(10**(side1 - 1))
hold = int(num_in)
 
fhand = open("cd4.txt", "w")
 
s = 0
chain = []
while int(hold) < 10**side1:
    while s == 0:
        A = mat(num_in, side)
        det = int(round(np.linalg.det(A)))
        if (det in chain or abs(det) in chain) or (det < 10 and det > -10):
            chain.append(det)
            break
        else:
            chain.append(det)
            num_in = str(abs(det))
            side = len(num_in)
    hold += 1
    num_in = str(hold)
    side = len(num_in)
    print(f'{hold - 1}, {chain}')
    fhand.write(f'{hold - 1}, {chain}\n')
    chain = []
 
fhand.close()
With that last modification both the text file and the output on the screen are the same.

All the best,