Wednesday, November 7, 2012

Write your prime numbers to primes_file.txt



This program was to experiment with writing to file.  It uses the last file set of PrimesTools.

First it works out the primes below 20, then uses that list as a parameter in the program to add on primes to an existing list and then finally writes to new file to the file.

What I learnt

  • using the write() method to write a string and a newline character to a file.
  • using the open() method to open a file - but more study of options needed
  • about file objects, returned when you open() a file I recall


What I need to do...


  • Check up on how the paths work and how to check about overwriting files etc
  • Probably my iteration could improve - maybe I dont need to use range() all the time
  • Check about how to examine properties of the file object created on opening the file.
  • Improve things by allowing proper input and functions call etc
  • Make this into a function itself to help extend my list of prime numbers.
  • Check if running in the IDE is slower than actually running off the python operating line...
  • Other options on the r




import PrimesTools

myPrimes = PrimesTools.listOfPrimes(20)
print("length of myprimes", len(myPrimes))

primesToWrite = PrimesTools.boundedListOfPrimes(myPrimes,100)
primesFile = open ("primes_file.txt","a")
for w in range(0,len(primesToWrite)):
    primesFile.write(str(primesToWrite[w])+"\n")
primesFile.close()

No comments:

Post a Comment