Difference between revisions of "FileIO"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 16: Line 16:
 
     print(str(i) + "; " + str(random_integer))
 
     print(str(i) + "; " + str(random_integer))
 
file.close()
 
file.close()
<syntaxhighlight lang="python" line='line'>
+
</syntaxhighlight>

Revision as of 18:45, 1 June 2024

Description

Write and read files using MircoPython.


Write to File

 1 # write 100 random values to a file
 2 import random
 3 import os
 4 
 5 file = open('datafile01.txt', 'a')
 6 for i in range(100):
 7     random_integer = random.randint(1, 1000)
 8     file.write(str(i) + "; " + str(random_integer) + "\n")
 9     print(str(i) + "; " + str(random_integer))
10 file.close()