mpremote cp fails with apostrophe in destination filename
Port, board and/or hardware
Any platform (Windows, Linux, macOS) - affects mpremote tool when copying files with apostrophe (') in the filename.
MicroPython version
- mpremote 1.27.0
- Tested against MicroPython unix port and ESP32
The issue is in mpremote itself, not in MicroPython firmware.
Reproduction
-
Create a test file with an apostrophe in the name:
mkdir unicode_test echo "test" > "unicode_test\O'zbek_Ismoilov.txt" -
Attempt to copy to MicroPython device with the same destination name:
mpremote cp "unicode_test\O'zbek_Ismoilov.txt" ":/O'zbek_Ismoilov.txt" -
Observe the error.
Expected behaviour
mpremote cp should successfully copy files with apostrophes in filenames. The apostrophe should be properly escaped when constructing Python commands to send to the MicroPython REPL.
Observed behaviour
The command fails with a SyntaxError because the apostrophe breaks the Python string literal sent to the REPL.
PS D:\mypython\unicode_mpy> mpremote cp "unicode_test\O'zbek_Ismoilov.txt" ":/O'zbek_Ismoilov.txt"
cp unicode_test\O'zbek_Ismoilov.txt :/O'zbek_Ismoilov.txt
mpremote: Error with transport:
Traceback (most recent call last):
File "<stdin>", line 1
SyntaxError: invalid syntax
Additional Information
mpremote sends filenames to MicroPython's REPL as Python string literals using single quotes. When a filename contains an apostrophe ', it prematurely closes the string:
# What mpremote probably sends:
open('/O'zbek_Ismoilov.txt', 'wb')
# ^ This closes the string prematurely!
# Result is invalid Python syntax
Affected Characters
Any filename containing:
- Single quote / apostrophe:
'(U+0027) - Possibly other quote-like characters that need escaping
Suggested Fix
Properly escape quotes when constructing Python commands to send to the REPL:
Option A: Escape single quotes
# Instead of:
cmd = f"open('{filename}', 'wb')"
# Use:
escaped_filename = filename.replace("\\", "\\\\").replace("'", "\\'")
cmd = f"open('{escaped_filename}', 'wb')"
Option B: Use double quotes for filenames
cmd = f'open("{filename}", "wb")'
# Note: would then need to escape double quotes in filenames
Option C: Use repr() for proper escaping
cmd = f"open({repr(filename)}, 'wb')"
Found via : https://github.com/Josverl/unicode_mpy
Code of Conduct
Yes, I agree
mpremote cp -r command fails with "mpremote: cp: -r not specified; omitting directory"
Port, board and/or hardware
ESP32 on a custom designed ESP32-s3 board
MicroPython version
MicroPython v1.25.0 on 2025-04-15; Generic ESP32S3 module with ESP32S3
Reproduction
- install mpremote using the instructions here.
- run the command
mpremote connect /dev/cu.usbserial-A5069RR4 cp -r configuration/ :configuration/to recursively copy theconfigurationdirectory to the board.
Expected behaviour
I expected the command to recursively copy all files within the configuration directory over to the board to a folder located at /configuration, and to create that folder if it doesn't exist. This expectation comes from the documentation on this page.
mpremote cp -r utils/ :utils/ + soft-reset repl
Same as above, but update the entire utils directory first.
Observed behaviour
The tool returns the following error:
$ mpremote connect /dev/cu.usbserial-A5069RR4 cp -r configuration/ :configuration/
cp configuration/ :configuration/
mpremote: cp: -r not specified; omitting directory
Additional Information
Running on a M1 MacBook Pro, macOS Sequoia 15.3.2.
Using mpremote 1.25.0.
Code of Conduct
Yes, I agree