Open topic with navigation
write
\examples\file_io\write.py
Writes data to a text file with input from the keyboard.
"""
When you press a keyboard button, the script will output the
current time and the button pressed to the file 'response.txt'
"""
import viz
viz.go()
import vizinfo
vizinfo.InfoPanel()
# Opens file 'response.txt' in write mode
file = open('response.txt', 'w')
# Define a function that saves data
def SaveData(currenttime, key):
# Create the output string
out = str(currenttime) + '\t' + key + '\n'
# Write the string to the output file
file.write(out)
# Makes sure the file data is really written to the harddrive
file.flush()
print(out)
# Define a function that is called every time a keyboard button is pressed
def mykeyboard(key):
# Calls the function SaveData and hands it the current time and key
SaveData(viz.tick(),key)
# Defines a callback for keyboard events
viz.callback(viz.KEYDOWN_EVENT, mykeyboard)