Python Forum
file.write not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file.write not working properly
#1
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
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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]
Reply
#7
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
Reply
#8
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.
Reply
#9
Ah, that did it. I totally forgot the parenthesis. Thanks.
Reply
#10
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 569 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  Last record in file doesn't write to newline gonksoup 3 365 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,374 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,523 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  PyAutogui write Dollar Sign Dutch keyboard not working alato 0 780 Nov-22-2022, 11:25 PM
Last Post: alato
  How to write in text file - indented block Joni_Engr 4 6,363 Jul-18-2022, 09:09 AM
Last Post: Hathemand

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020