Difference between revisions of "FileIO"
Jump to navigation
Jump to search
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
Write and read files using MircoPython. | Write and read files using MircoPython. | ||
Line 46: | Line 45: | ||
print(columns[1] + " is the value at: " + columns[0]) | print(columns[1] + " is the value at: " + columns[0]) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | == Get and Put Files Between Microcontroller and Local Machine == | ||
+ | Files can be copied via the command line tool [https://github.com/scientifichackers/ampy Adafruit MicroPython tool] (ampy). | ||
+ | <syntaxhighlight lang="Bash"> | ||
+ | pip3 install adafruit-ampy | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | To <b>get</b> a file from the microcontroller to the local machine, use the following command: | ||
+ | <syntaxhighlight lang="Bash"> | ||
+ | ampy --port /dev/tty.usbmodem143101 get FILE_NAME_OF_REMOTE_FILE.FILE_EXTENSION FILE_NAME_FOR_THE_LOCAL_FILE.FILE_EXTENSION | ||
+ | </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> | ||
+ | |||
+ | Alternatively, you can use the [[Thonny IDE]]. | ||
+ | |||
+ | [[Category:MicroPython]] |
Latest revision as of 09:42, 12 June 2024
Write and read files using MircoPython.
Write to File[edit]
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()
Or and alternative: here the file is closed automatically when the block inside the with statement is exited.
1 # write 100 random values to a fileimport random
2
3 # Use a context manager to handle the file
4 import random
5 import os
6
7 with open('datafile02.txt', 'a') as file:
8 for i in range(100):
9 random_integer = random.randint(1, 1000)
10 file.write(str(i) + "; " + str(random_integer) + "\n")
11 print(str(i) + "; " + str(random_integer))
Read to File[edit]
1 # Read the file
2 import os
3
4 with open('datafile01.txt', 'r') as file:
5 for line in file:
6 columns = line.strip().split('; ')
7 print(columns[1] + " is the value at: " + columns[0])
Get and Put Files Between Microcontroller and Local Machine[edit]
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
Alternatively, you can use the Thonny IDE.