I have a problem with a Python program. It's a simple program that writes prime numbers to a file. I wrote it to practice Python. Everything goes fine until several hunder lines are written. Then the writing stops although the program continues to run until the highest number is reached.

I debugd the program and found that the write\_prime function is called for each prime number, but is doesn't write to the file. I tested the program under Linux and under Windows7 and on both systems the same problem occurs, although on Windows it writes less lines. The reason for that seems to me that writing stops after 8192 characters (1 block) and Windows uses two characters for end of line, so it takes less lines to fill a block.

I wrote a similar program, but in that version I only write to the file without reading it (I use a list to store the prime numbers and loop through the list instead of through the file). In that program I don't have this problem, so I think it has something to do with the fact that the programs reads and writes to the same file.

Who can help me?

Here I show the output in Both Linux and Windows:

Under Linux (Ubuntu 14,10) with highest number is 8000:

1;2;0

2;3;1

3;5;2

4;7;2

…

…

750;5693;4

751;5701;8

752;51007;7993;30

The block of 8192 bytes ends after position 5 at line 752.
After that we see one more prime number:
the 1007th prime number and the number itself is 7993.

Under windows7 with highest number is 8000:

The file starts with the same numbers 2, 3, 5, 7 etc. and ends with

…

…

689;5171;4

690;5179;8

691;511007;7993;30

So the file is about 60 lines shorter. It is 8,206 bytes. I think this is because windows ends each line with '\r\n' (2 characters) and Linux ends each line with '\n' (1 character).

So in both cases writing ends after one block.

Here is the full program:

```
"""  
Calculate prime numbers  

written and tested with python 3.4.2  

- enter the highest number   
- calculate all prime numbers up and until the highest number  

It is possible to run the program more often. It checks out the highest prime  
number in file primenumber.lst and continues from there.  

The file primenumbers.lst consists of lines with 3 fields separated by ';':  
- counter  
- prime number  
- distance to the previous prime number  

"""

from os.path import isfile  

def write_prime(counter, prime, distance):  
    f.write('%d;%d;%d\n' % (counter, prime, distance))  
    return  

"""  
position the file at the last position with seek(0,2) (2 = last position  
in file) and go back with read(1) and seek(position,0) until you found the \n of  
the previous record. Than read the next line (the last line of the file) with  
readline() and split the line into the three fields.  
If the file was not found, then the program runs for the first time.  
In that case you write the prime numbers 2, 3 and 5 to the file.  
You write these three prime number, so we can skip the other numbers that end  
with 5 to save time, for those are not prime numbers.  
"""  

if isfile("primenumber.lst"):  
    f = open("primenumber.lst", "r")  
    f.seek(0,2)  
    position = f.tell() - 2  
    f.seek(position, 0)  

    while f.read(1) != '\n':  
        position -= 1  
        f.seek(position,0)  
    line = str(f.readline()).split(';')  
    print(line)  
    counter = int(line[0])  
    previous = int(line[1])  
else:  
    f = open("primenumber.lst", "w")  
    write_prime(1, 2, 0)  
    write_prime(2, 3, 1)  
    write_prime(3, 5, 2)  
    counter = 3  
    previous = 5  

f.close()  

print('The highest prime number so far is %s' % str(previous))  
startnumber = previous + 1  
highest = int(input("Highest number: "))  

"""  
Walk through all the numbers until the highest number (entered at the screen).  
Skip the numbers that end with 0, 2, 4, 5, 6 or 8, for those are no primes.  

Divide each number by all the prime numbers you found before, until you reach  
the root of the number.  
If the modulo of one division is 0, the number is no prime number and you  
break out of the 'for line in f' loop.  
If it is a prime number write it to the file and break out of the loop.  
"""  

f = open("primenumber.lst", "r+")  

for i in range(startnumber,highest + 1):      # loop to the highest number  
    if str(i)[-1] in ('1', '3', '7', '9'):  
        f.seek(0)  
        root = int(i ** 0.5)  
        for line in f:                       # read all primes in the file  
            line_str = line.split(';')  
            x = int(line_str[1])  

            if i % x == 0:   
                break                   
            if x > (root):   
                counter += 1  
                distance = i - previous  
                previous = i  
                write_prime(counter, i, distance)  
                f.flush()  
                break                 

f.close()

```