-8

I coded a Python tool that calculates prime numbers within a given range. Then I decided that copying the numbers from the shell, creating a txt file, and pasting them every single time is a tad troublesome, and it would be very convenient if i can get the tool to insert the primes into a text file.

I tried this:

def calc():
    while True:
        x = int(input("Please specify the lower end of the range...."))
        y = int(input("Please specify the upper end of the range...."))
        for n in range (x,y):
            if all(n%i!=0 for i in range (2,n)):
                a=[]
                a.append(n)
                fo = open('primes.txt', 'w')
                print (">>>Writing the values to primes.txt...")
                print ("##########Calculated by my prime calculator##########", file = fo)
                print ("", file = fo)
                print ((a), file = fo)
                fo.close
        s = input('To do another calculation input yes, to quit input anything else...')
        if s == 'yes':
            continue    
        else:
            break
calc()

EDIT:

Using with open("primes.txt", "a") as fo I solved the problem

However, I couldn't get Python to save n values to memory and append them to a growing list.

You guys are awesome. That part about Python being stupid was an attempt at a humorous start lol.

  • As a side note, `fo.close` doesn't do anything at all. To call the method, you would need `fo.close()`, but you should actually use `with open(...) as fo` to have it automatically closed. – Sven Marnach Oct 21 '16 at 17:51

1 Answers1

3
fo = open('primes.txt', 'w') #tells python to open the file and delete everything in it

perhaps you want

fo = open('primes.txt', 'a') # tells python to append to the file

really you should not be doing this at all you should use with to safely open your file and do it once only outside of the loop

with open("primes.txt","w") as fo:
    for n in range (x,y):
        if all(n%i!=0 for i in range (2,n)):
            a=[]
            a.append(n)             
            print (">>>Writing the values to primes.txt...")
            print ("##########Calculated by my prime calculator##########", file = fo)
            print ("", file = fo)
            print ((a), file = fo)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    you should use with open or close the file descriptor – midori Oct 21 '16 at 17:47
  • 1
    While this kind of works, it's not the right solution anyway. You shouldn't reopen the file for each prime you are writing. Instead, either store them in memory and right them when you are finished, or open the file once before you start. – Sven Marnach Oct 21 '16 at 17:48
  • i was trying to provide the smallest change possible and explain why the code was not working ... this answer does both those things... I guess i can go off on a tangent about best practices – Joran Beasley Oct 21 '16 at 17:49
  • A better approach might be open the file once, *outside* the `for` loop, and just write to it inside, rather than opening and closing it repeatedly. (Not that the file is actually ever closed because of the missing parens on `fo.close`...) – kindall Oct 21 '16 at 17:50
  • Me calling Python stupid or moron is just my attempt at a humorous start.. I obviously wasn't trying to insult a programming language to. Plus Python's my favorite. – Python_fan Oct 21 '16 at 17:53
  • 1
    kindall: The file is closed the next time `fo = open('primes.txt', 'w')` is executed, since that's the time the old file object gets garbage collected. – Sven Marnach Oct 21 '16 at 17:53