how to realize os.walk() command in micropython
i need to take all content in flash(files in other directories)
but os has not that command
os.walk('.') in micropython
Re: os.walk('.') in micropython
You can use os.listdir() to get a list of all files in a directory and os.stat to determine which of those are directories and recurse.
This is what rshell does for its rsync command.
You could also take the os.walk implementation from here: https://github.com/micropython/micropyt ... __.py#L147
This is what rshell does for its rsync command.
You could also take the os.walk implementation from here: https://github.com/micropython/micropyt ... __.py#L147
-
- Posts: 11
- Joined: Sat Apr 20, 2019 8:44 am
Re: os.walk('.') in micropython
THANKS!
i wrote changed code:
i changed isdir function(tryes open a file. you cant open a dir)
i wrote changed code:
Code: Select all
import uos
files=[]
dirs=[]
def S_ISDIR(fname):
try:
f = open(fname, "r")
exists = True
f.close()
except OSError:
exists = False
return(exists)
def walk(top):
for dirent in uos.ilistdir(top):
#print(dirent)
mode = dirent[1] << 12
fname = dirent[0]
if S_ISDIR(fname):
dirs.append(fname)
else:
files.append(fname)
yield top, dirs, files
for d in dirs:
yield from walk(top + "/" + d)
for top, dirs, files in walk('/sd'):
print(dirs,files)