Remove directory that is not empty?
-
- Posts: 5
- Joined: Tue Dec 31, 2019 5:42 pm
Remove directory that is not empty?
Hi! I have a very simple question, that hopefully has a very simple answer. I need the functionality of removing a directory that is not empty on my device, but I can not figure out how.
Apparently there is no shutil.rmtree() or os.walk() in MicroPython. So how would I do it?
Apparently there is no shutil.rmtree() or os.walk() in MicroPython. So how would I do it?
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: Remove directory that is not empty?
How about a recursive function based on os.ilistdir() which calls itself plus os.rmdir() for directories and os.remove() for files?
Regards, Jan
Regards, Jan
Re: Remove directory that is not empty?
There's an implementation of some parts of shutil in micropython-lib: https://github.com/micropython/micropyt ... /shutil.py
Re: Remove directory that is not empty?
Code: Select all
import os
def rmdir(dir):
for i in os.listdir(dir):
os.remove('{}/{}'.format(dir,i))
os.rmdir(dir)
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA
Re: Remove directory that is not empty?
What happens when there are subdirectories?tiny wrote: ↑Fri Jan 03, 2020 8:29 amCode: Select all
import os def rmdir(dir): for i in os.listdir(dir): os.remove('{}/{}'.format(dir,i)) os.rmdir(dir)
Re: Remove directory that is not empty?
I made this one once, which recurses into subdirectories
Code: Select all
def rm(d): # Remove file or tree
try:
if os.stat(d)[0] & 0x4000: # Dir
for f in os.ilistdir(d):
if f[0] not in ('.', '..'):
rm("/".join((d, f[0]))) # File or Dir
os.rmdir(d)
else: # File
os.remove(d)
except:
print("rm of '%s' failed" % d)
Re: Remove directory that is not empty?
(Edit, it occurs to me now how bad it is to name my func the same as os modules'. Especially when trying to help/suggest code to others. changes that to 'rmvdir'. )
os.ilistdir is interesting, I didnt know it was a thing until your post. Funny that uPy has both it, and the usual listdir.
The help says:
Either way, takes the Pepsi challenge:
How cool, its recursive! woot. I also didn't really test it. But - fun! Meanwhile, steals @roberthh's rendition for personal stash Cheers
os.ilistdir is interesting, I didnt know it was a thing until your post. Funny that uPy has both it, and the usual listdir.
The help says:
...but when I ilistdir, i get either 32678 for a file, or 16384 for a dir:This function returns an iterator which then yields tuples corresponding to the entries in the directory that it is listing. With no argument it lists the current directory, otherwise it lists the directory given by dir.
The tuples have the form (name, type, inode[, size])
[..]
type is an integer that specifies the type of the entry, with 0x4000 for directories and 0x8000 for regular files;
Code: Select all
for i in os.ilistdir('.'):
print(i)
>>> ('boot.py', 32768, 0, 1635)
>>> ('lib', 16384, 0, 0)
Code: Select all
import os
def rmvdir(dir):
for i in os.ilistdir(dir):
if i[1] == 16384:
rmvdir('{}/{}'.format(dir,i))
elif i[1] == 32678:
os.remove('{}/{}'.format(dir,i[0]))
os.rmdir(dir)
-
- Posts: 5
- Joined: Tue Dec 31, 2019 5:42 pm
Re: Remove directory that is not empty?
Sorry that I haven't posted this sooner but I thank you all for the interpretations and code! I will use Robert's in my application.
Many thanks!
Many thanks!
- MostlyHarmless
- Posts: 166
- Joined: Thu Nov 21, 2019 6:25 pm
- Location: Pennsylvania, USA