Created
July 17, 2022 12:23
-
-
Save romilly/5a1ff86d1e4d87e084b76d5651f23a40 to your computer and use it in GitHub Desktop.
Delete all files and directories from a micropython file-system (including .py files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ***WARNING*** | |
# Running this file will delete all files and directories from the micropython device it's running on | |
# If you run keep_this=False it will delete this file as well. | |
# see https://docs.micropython.org/en/latest/library/os.html for os function list | |
import os | |
def _delete_all(directory='.', keep_this=True): | |
try: | |
import machine | |
except: | |
# not a micropython board so exit gracefully | |
print('Not a micro-python board! Leaving it well alone.') | |
return | |
for fi in os.ilistdir(directory): | |
fn, ft = fi[0:2] # can be 3 or 4 items returned! | |
if keep_this and fn == '_nuke.py': | |
continue | |
fp = '%s/%s' % (directory, fn) | |
print('removing %s' % fp) | |
if ft == 0x8000: | |
os.remove(fp) | |
else: | |
_delete_all(fp) | |
os.rmdir(fp) | |
_delete_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script. Thanks a lot.
The only thing is that it also removes
boot.py,
which is probably not desirable.I have adjusted the script a bit to remove all the files from the board except
boot.py
: