-1

I am trying to write to a text file every 5 seconds from the data that is being created by this code.

import time
x = 0
while x < 60:
    x += 1
    y = x + 2
    print(x,y)
    time.sleep(1)

    while True:
        f = open("outputtest.txt", "a+")
        f.write(str(x) + "\n")
        time.sleep(5)

What I basically want it to do is print the x value of the script every 5 seconds to a data file

Edit: Data is not being outputted to a file currently and that is what i am trying to figure out how to do.

the output would ideally be 5 10 15 20 etc

irobinso
  • 1
  • 3
  • 3
    You haven't asked a question. Is there something wrong with the code you've shown? – Ned Batchelder May 01 '17 at 17:41
  • What is the data being created by this code? Numbers 1 to 59? What is `y` for? – MrJLP May 01 '17 at 17:51
  • The data does not get outputted to the text file. – irobinso May 01 '17 at 17:57
  • The data being created is just a test in order to see if I can do it. What my actual goal is going to be outputting a voltage from a digital to analog converter from a raspberry pi. X in this case is just a variable and will be later inputted as voltage – irobinso May 01 '17 at 17:59

1 Answers1

0

If your file operations going to take a long time before closing the file, you should considering to force the write operation by calling the flush methode of the file object. This will write several bufferd data to the file.

See also: How often does python flush to a file?

Community
  • 1
  • 1
veeman
  • 780
  • 1
  • 8
  • 17