# Test to read from a text file.
#
# After the script has finished in Wokwi,
# the state of the project returns to its initial state.
# Any written data to a text file is only temporary.
#
# There is a workaround.
# Output the text data to the Serial Monitor
# and then copy that into a text file
# to become part of the project.
#
import time
from os import listdir, chdir
from machine import Pin
import math
time.sleep(0.1) # Wait for USB to become ready
print("-----------------------------------------------------------")
print("Raspberry Pi Pico.")
print("Testing UTF-8: 🟠 🟦 😎 😺 𝄞 ⛟")
print("I found these files: {}".format(listdir()))
file = open('newFile.txt', 'w')
file.write('This is created in the script')
file.close()
print("With the new file : {}".format(listdir()))
print("Reading contents: ", end='')
file = open('myData.txt', 'r')
contents = file.read()
file.close()
print("\"",contents.replace("\n", ", "), "\"", sep='')
print("Read line by line and extract a number:")
fp = open('myData.txt', 'r')
for i, line in enumerate(fp):
cleanline = line.replace("\n", "")
number = float(line.split()[0])
result = math.sqrt(abs(number)) / math.pi
note = ""
if number < 0:
note = "*"
print("{0:03d} : {1:25} {2:6f} {3}".format(i,cleanline,result,note))
fp.close()
print("-----------------------------------------------------------")