0

I'm trying to read all files with .asm and .py extensions in an sd card using MicroPython.

I checked the answers in this question but they don't work with MicroPython.

MicroPython doesn't have glob nor pathlib and when using os library and try this code:

for file in os.listdir('/sd'):
        filename = os.fsdecode(file)
        if filename.endswith(".asm") or filename.endswith(".py"):
            print(filename)

I get this error 'module' object has no attribute 'fsdecode'

How can I get this to work with MicroPython?

Salahuddin
  • 1,617
  • 3
  • 23
  • 37

2 Answers2

2

For just a shallow listdir you do not need the fsdecode, in fact it is not part of the MicroPython os module Remember that MicroPython has an optimized sub-set of modules and methods.

for filename in os.listdir("/sd"):
    if filename.endswith(".asm") or filename.endswith(".py"):
        print(filename)

Actually to avoid subfolders you should check the type, which can be found by using os.ilistdir

for entry in os.ilistdir("/sd"):
    # print(entry)
    if entry[1] == 0x8000:
        filename = entry[0]
        if filename.endswith(".asm") or filename.endswith(".py"):
            print(filename)
Jos Verlinde
  • 1,468
  • 12
  • 25
  • Thanks. This did the job. Could you please explain what does `0x8000`mean? – Salahuddin Feb 03 '22 at 14:21
  • Well, it worked partly. I had two files in the folder. It processed the two files. Then processed the first file again. it printed its name proceeded with `_`. And the file size and content are completely wrong. – Salahuddin Feb 03 '22 at 14:26
  • the answer to both your questions is in the link provided in the answer. May I suggest that you read though the few paragraphs there ? – Jos Verlinde Feb 04 '22 at 11:36
  • wrt to the additional file : what is the output of just `os.listdir('/sd')`. if that also shows the file with the underscore , then perhaps your (PCs?) view of the SD cards content hide some parts of the underlying filesystem. – Jos Verlinde Feb 04 '22 at 11:38
  • Sorry, i should have read the docs. I'll do. The output of `os.listdir('/sd')` showed that the folder (which is an sd card mounted on raspberry pi pico controller) has for each normal file another file with the same name proceeded with underscore. I filled the sd card using my macbook. In mac, when I viewed the hidden files in the sd card, I didn't find the files prefixed with underscore. Any suggestion how to solve it, or why do I see this when accessing the files on the rpi but not on the pc? Thanks – Salahuddin Feb 04 '22 at 20:45
  • 1
    Your answer solved my main problem. I made a workaround to avoid the _ files by checking the size of the read file before processing it. This worked for me. Thanks :) – Salahuddin Feb 05 '22 at 07:58
  • 1
    the `._` files are created by the Mac for the Mac in order to store Mac resource forks on file systems such as FAT: https://apple.stackexchange.com/a/14981 and https://www.cnet.com/tech/computing/invisible-files-with-prefix-are-created-on-some-shared-volumes-and-external-disks/ – Jos Verlinde Feb 06 '22 at 15:26
0

your on the right track, os.listdir works just fine on any version of MicroPython, but does not recurse the sub-folders.

so you need to distinguish a folder from a filenode as in the below.

"""Clean all files from flash 
use with care ; there is no undo or trashcan 
"""
import uos as os 

def wipe_dir( path=".",sub=True):
        print( "wipe path {}".format(path) )
        l = os.listdir(path)
        l.sort()
        #print(l)
        if l != ['']:
            for f in l:
                child = "{}/{}".format(path, f)
                #print(" - "+child)
                st = os.stat(child)
                if st[0] & 0x4000:  # stat.S_IFDIR
                    if sub:
                        wipe_dir(child,sub)
                        try:
                            os.rmdir(child)
                        except:
                            print("Error deleting folder {}".format(child))
                else: # File
                    try:  
                        os.remove(child)
                    except:
                        print("Error deleting file {}".format(child))

# wipe_dir(path='/')
Jos Verlinde
  • 1,468
  • 12
  • 25
  • Thanks @Jos for your answer. I'm not trying to delete any files and I don't have nested folders. It is just one folder. I'd like to open/read all the files that have extension *.py or *.asm in this folder. – Salahuddin Feb 02 '22 at 16:42
  • I assumed that all actually meant all , not only the files in this folder – Jos Verlinde Feb 02 '22 at 21:16