import os.path error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
66229
Posts: 2
Joined: Sun Jun 19, 2016 3:16 pm

import os.path error

Post by 66229 » Sun Jun 19, 2016 3:46 pm

Can you please inform me if the os.path module has been implemented in pybv10-2016-06-18-v1.8.1-65-gcbbd0a.dfu as when I import os.path I receive "ImportError: no module named 'os'.

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

Re: import os.path error

Post by Roberthh » Sun Jun 19, 2016 6:15 pm

os.path is not implemented in MicroPython. The module uos contains a sobset of Python's module os. On Pyboard, os is the alias of uos. For Linux MicroPython there exists a micropython-lib (https://github.com/micropython/micropython-lib) with a lot of extensions, including os.path. But that's not portable to PyBoard.

66229
Posts: 2
Joined: Sun Jun 19, 2016 3:16 pm

Re: import os.path error

Post by 66229 » Sun Jun 19, 2016 6:55 pm

Many thanks for the prompt response...

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

Re: import os.path error

Post by Roberthh » Mon Jun 20, 2016 5:11 am

@66229: Update - a closer look into micropython-lib showed, that there is a os.path implementation, which should be easily portable to Pyboard. It look that only o.path.exists() and os.path.isdir() would have to be adapted, and expanduser() is hardly applicable, but could also modified to return a reasonable value. But that should be possible using the os.stat() call. If you need specific methods, like split(), dirname() or basename(), you can take them from there.

User avatar
smazcontrol
Posts: 1
Joined: Sun Jul 09, 2017 1:20 pm

Re: import os.path error

Post by smazcontrol » Sun Jul 09, 2017 1:25 pm

I do not see possibility in MicroPython to check it simple like os.path.isfile
then use this code to solve in alternative way

len([item for item in os.listdir() if fname==item])>0

ref. [url]https://forum.pycom.io/topic/196/simple-file-checking[/url]

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: import os.path error

Post by deshipu » Sun Jul 16, 2017 10:19 pm

Code: Select all

len([item for item in os.listdir() if fname==item])>0
Wouldn't it be better to just do:

Code: Select all

fname in os.listdir()

Post Reply