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
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 249 Oct-21-2019, 11:28 AM
Last Post: kryptomatrix
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 141 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  How do I write class objects to a file in binary mode? Exsul1 7 462 Sep-14-2019, 09:33 PM
Last Post: snippsat
  how do i write a cross-platform relative file path? pseudo 2 154 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  write to txt file one line antmar904 3 287 Aug-16-2019, 02:40 PM
Last Post: Axel_Erfurt
  read text file and write into html with correct link jacklee26 4 287 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  hatching not working properly with matplotlib Staph 3 193 Jul-28-2019, 07:17 AM
Last Post: ThomasL
  Writing the Path to a file properly dcw9996 2 284 Jul-01-2019, 11:26 PM
Last Post: dcw9996
  Working with LibreOffice file FASB 0 261 Jun-13-2019, 01:23 PM
Last Post: FASB
  is it possible to use python to read and write from a jsx file? nadavrock 3 383 Jun-08-2019, 02:59 AM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)