Remove directory that is not empty?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
techykermit
Posts: 5
Joined: Tue Dec 31, 2019 5:42 pm

Remove directory that is not empty?

Post by techykermit » Thu Jan 02, 2020 6:46 pm

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?

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Remove directory that is not empty?

Post by MostlyHarmless » Thu Jan 02, 2020 9:42 pm

How about a recursive function based on os.ilistdir() which calls itself plus os.rmdir() for directories and os.remove() for files?


Regards, Jan

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Remove directory that is not empty?

Post by jimmo » Fri Jan 03, 2020 5:34 am

There's an implementation of some parts of shutil in micropython-lib: https://github.com/micropython/micropyt ... /shutil.py

User avatar
tiny
Posts: 6
Joined: Tue Dec 31, 2019 1:26 am

Re: Remove directory that is not empty?

Post by tiny » Fri Jan 03, 2020 8:29 am

Code: Select all

import os
def rmdir(dir):
    for i in os.listdir(dir):
        os.remove('{}/{}'.format(dir,i))
    os.rmdir(dir)

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Remove directory that is not empty?

Post by MostlyHarmless » Fri Jan 03, 2020 12:53 pm

tiny wrote:
Fri Jan 03, 2020 8:29 am

Code: Select all

import os
def rmdir(dir):
    for i in os.listdir(dir):
        os.remove('{}/{}'.format(dir,i))
    os.rmdir(dir)
What happens when there are subdirectories?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Remove directory that is not empty?

Post by Roberthh » Fri Jan 03, 2020 1:31 pm

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)

User avatar
tiny
Posts: 6
Joined: Tue Dec 31, 2019 1:26 am

Re: Remove directory that is not empty?

Post by tiny » Sat Jan 04, 2020 12:49 am

(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:
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;
...but when I ilistdir, i get either 32678 for a file, or 16384 for a dir:

Code: Select all

for i in os.ilistdir('.'):
    print(i)

>>> ('boot.py', 32768, 0, 1635)
>>> ('lib', 16384, 0, 0)
Either way, takes the Pepsi challenge:

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)
How cool, its recursive! woot. I also didn't really test it. But - fun! :) Meanwhile, steals @roberthh's rendition for personal stash ;) Cheers

techykermit
Posts: 5
Joined: Tue Dec 31, 2019 5:42 pm

Re: Remove directory that is not empty?

Post by techykermit » Tue Jan 07, 2020 9:26 am

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!

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Remove directory that is not empty?

Post by MostlyHarmless » Tue Jan 07, 2020 4:13 pm

tiny wrote:
Sat Jan 04, 2020 12:49 am
...but when I ilistdir, i get either 32678 for a file, or 16384 for a dir:
What are 16384 and 32768 in hex?

Post Reply