upip micropython-os do no work properly

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: upip micropython-os do no work properly

Post by Roberthh » Fri Jun 21, 2019 6:26 am

Now the final version of this itsy-bitsy thing as a general remove:

Code: Select all

def rmtree(d):  # Remove file or tree

    try:
        if os.stat(d)[0] == 16384:  # Dir
            for f in os.ilistdir(d):
                rmtree("/".join((d, f[0])))  # File or Dir
            os.rmdir(d)
        else:  # File
            os.remove(d)
    except:
        print("rm of '%s' failed" % d)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Er, rmtree do no work properly

Post by pythoncoder » Fri Jun 21, 2019 7:03 pm

This last version won't remove a directory tree which is populated with files. I think the problem is the path in the line

Code: Select all

            os.remove(d)
Peter Hinch
Index to my micropython libraries.

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

Re: upip micropython-os do no work properly

Post by Roberthh » Fri Jun 21, 2019 7:59 pm

Cannot confirm. That was one of my test cases:

Code: Select all

test/
   a
   b
   c
   test/
        a
        b
os.remove(d) is meant for files. If will fail if the file is not removable of if it is not a file, which both does not happen on pyboard, esp32, esp8266.
Edit: With Linux you get trouble because it does not exclude '.' and '..' dirs
Edit 2: So it should be then:

Code: Select all

def rm(d):  # Remove file or tree

    try:
        if os.stat(d)[0] == 16384:  # Dir
            for f in os.ilistdir(d):
                if f[0] != '.' and f[0] != '..':
                    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
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: upip micropython-os do no work properly

Post by Roberthh » Sat Jun 22, 2019 6:12 am

@pythoncoder: It's hard to kill that little fly. The problem you found is caused by stat not returning 16384 for a directory on linux. SO the code has to be changed a litte bit again:

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] != '.' and f[0] != '..':
                    rm("/".join((d, f[0])))  # File or Dir
            os.rmdir(d)
        else:  # File
            os.remove(d)
    except:
        print("rm of '%s' failed" % d)

Post Reply