5

If I run the following code:

from pathlib import Path
path = Path('data/mnist')
path.ls()

I get the following error:

AttributeError: ‘PosixPath’ object has no attribute ‘ls’

Looking at the Path class in pathlib, I find:

def __new__(cls, *args, **kwargs):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args, init=False)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
                                      % (cls.__name__,))
        self._init()
        return self

I'm guessing this means it will run PosixPath instead, which is:

class PosixPath(Path, PurePosixPath):
    """Path subclass for non-Windows systems.

    On a POSIX system, instantiating a Path should return this object.
    """
    __slots__ = ()

Not too sure what this means.

And actually, I can't find Path.ls() at all in the pathlib source code. Does this make sense? The coding tutorial I'm following used it (on a windows machine).

Jacques Thibodeau
  • 859
  • 1
  • 8
  • 21

1 Answers1

4

If one reads the documentation of the pathlib module one can confirm that, indeed, the class Path has no method ls. However, if your objective is to list files on a give directory, you could use the glob method like this:

from pathlib import Path

DIR = '.'
PATHGLOB = Path(DIR).glob('./*')
LS = [fil for fil in PATHGLOB]

I think this code snippet achieves the same that the code in your tutorial.

EDIT:

The fastai module does implement the ls method like this:

Path.ls = lambda x: [o.name for o in x.iterdir()]

I think the observed behavior is the result of the import * in the Jupyter notebook of the tutorial. This can be corroborated with the following code snippet:

from fastai import data_block

path = data_block.Path('.')
path.ls()
panadestein
  • 1,241
  • 10
  • 21
  • Yeah, that is good! And I know how to get around the issue I'm having, but I wanted to figure out why someone can use ls() on the windows machine, and I can't seem to with Google Colab. – Jacques Thibodeau Jun 07 '20 at 21:47
  • @JacquesThibodeau I have improved my answer. I believe the observed behavior is the result of the import `*` in the Jupyter notebook. – panadestein Jun 07 '20 at 22:10
  • So, it works if I use `from fastai.vision import *`. It seems I need to specifically import one of the applications for it to work. I realized this because DataBunch wasn't working either until I imported `fastai.vision`. Not sure if this is a bug or not. – Jacques Thibodeau Jun 07 '20 at 22:40