Difference between revisions of "FileIO"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
(Created page with "= Description = Write and read files using MircoPython. = Write to File = <syntaxhighlight lang="python" line='line'> # write 100 random values to a file import random impor...")
 
Line 4: Line 4:
  
 
= Write to File =
 
= Write to File =
 +
 
<syntaxhighlight lang="python" line='line'>
 
<syntaxhighlight lang="python" line='line'>
 
# write 100 random values to a file
 
# write 100 random values to a file

Revision as of 19:44, 1 June 2024

Description

Write and read files using MircoPython.


Write to File

<syntaxhighlight lang="python" line='line'>

  1. write 100 random values to a file

import random import os

file = open('datafile01.txt', 'a') for i in range(100):

   random_integer = random.randint(1, 1000)
   file.write(str(i) + "; " + str(random_integer) + "\n")
   print(str(i) + "; " + str(random_integer))

file.close() <syntaxhighlight lang="python" line='line'>