Bottom Page

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
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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]
Quote
#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
Quote
#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.
Quote
#9
Ah, that did it. I totally forgot the parenthesis. Thanks.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Write SQLite query result to file hjk6734 1 67 May-27-2020, 12:17 PM
Last Post: menator01
  Append JSON's and write to file faqsap 4 178 May-15-2020, 04:20 PM
Last Post: faqsap
  Print/write to file function tpolim008 4 217 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Write object to file medatib531 7 232 Apr-01-2020, 06:25 PM
Last Post: BrendanD
  Replace XML tag and write to the same file krish216 4 278 Mar-26-2020, 07:39 AM
Last Post: snippsat
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 330 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  What mode should i open the file to write into a *.msh file Dhanya 0 301 Jan-09-2020, 08:27 AM
Last Post: Dhanya
  how to write messages from command window to log file SriRajesh 1 360 Dec-25-2019, 03:57 PM
Last Post: ibreeden
  Write the XML file from elementtree with hexa decimal encoding Dillibabu 4 281 Dec-24-2019, 10:10 AM
Last Post: Dillibabu
  write an integer to file using a function xvkxvo 4 392 Dec-10-2019, 11:27 PM
Last Post: xvkxvo

Forum Jump:


Users browsing this thread: 1 Guest(s)