FileIO: Difference between revisions

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
No edit summary
Line 46: Line 46:
</syntaxhighlight>
</syntaxhighlight>


== Copy File from Microcontroller to Local Machine ==  
== Get and Put File from Microcontroller to Local Machine ==  
Files can be copied via the command line tool [https://github.com/scientifichackers/ampy Adafruit MicroPython tool] (ampy).
Files can be copied via the command line tool [https://github.com/scientifichackers/ampy Adafruit MicroPython tool] (ampy).
<syntaxhighlight lang="Bash">
<syntaxhighlight lang="Bash">
Line 52: Line 52:
</syntaxhighlight>
</syntaxhighlight>


To copy a file from the microcontroller to the local machine use the following command:
To <b>get</b> a file from the microcontroller to the local machine, use the following command:
<syntaxhighlight lang="Bash">
<syntaxhighlight lang="Bash">
ampy --port /dev/tty.usbmodem143101 get FILE_NAME_OF_REMOTE_FILE.FILE_EXTENSION FILE_NAME_FOR_THE_REOTE_FILE.FILE_EXTENSION  
ampy --port /dev/tty.usbmodem143101 get FILE_NAME_OF_REMOTE_FILE.FILE_EXTENSION FILE_NAME_FOR_THE_LOCAL_FILE.FILE_EXTENSION  
</syntaxhighlight>
</syntaxhighlight>
To <b>put</b> a file to the microcontroller from the local machine, use the following command:
<syntaxhighlight lang="Bash">
ampy --port /dev/tty.usbmodem143101 put FILE_NAME_OF_THE_LOCAL_FILE.FILE_EXTENSION FILE_NAME_FOR_REMOTE_FILE.FILE_EXTENSION
</syntaxhighlight>
[[Category:MicroPython]]
[[Category:MicroPython]]

Revision as of 14:07, 11 June 2024

Write and read files using MircoPython.


Write to File

# 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()


Or and alternative: here the file is closed automatically when the block inside the with statement is exited.

# write 100 random values to a fileimport random

# Use a context manager to handle the file
import random
import os

with open('datafile02.txt', 'a') as file:
    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))

Read to File

# Read the file 
import os

with open('datafile01.txt', 'r') as file:
    for line in file:
        columns = line.strip().split('; ')
        print(columns[1] + " is the value at: " + columns[0])

Get and Put File from Microcontroller to Local Machine

Files can be copied via the command line tool Adafruit MicroPython tool (ampy).

pip3 install adafruit-ampy

To get a file from the microcontroller to the local machine, use the following command:

ampy --port /dev/tty.usbmodem143101 get FILE_NAME_OF_REMOTE_FILE.FILE_EXTENSION FILE_NAME_FOR_THE_LOCAL_FILE.FILE_EXTENSION

To put a file to the microcontroller from the local machine, use the following command:

ampy --port /dev/tty.usbmodem143101 put FILE_NAME_OF_THE_LOCAL_FILE.FILE_EXTENSION FILE_NAME_FOR_REMOTE_FILE.FILE_EXTENSION