Is there a bug in `get_files` and `get_image_files`?

According to the documentation, it appears to me that get_files should follow symbolic links:

get_files (path, extensions=None, recurse=True, folders=None, followlinks=True)

But I noticed that this is not happening in my setup. In my home directory I have a symbolic link for a datasets directory mounted in another disk:

$ ls -la ~/datasets
lrwxrwxrwx. 1 fredguth fredguth 29 Oct  8 21:40 
/home/fredguth/datasets -> /run/media/fredguth/datasets/

When I try:

path = Path("/run/media/fredguth/datasets/binary-image-segmentation")
f = get_files(path)
len(f)

It returns 137705.

But when I try

linkpath = Path("~/datasets/binary-image-segmentation")
lf = get_files(linkpath)
len(lf)

It return 0.

I also noticed that there is no test for the argument followlinks:

t3 = get_files(path/'train'/'3', extensions='.png', recurse=False)
t7 = get_files(path/'train'/'7', extensions='.png', recurse=False)
t  = get_files(path/'train', extensions='.png', recurse=True)
test_eq(len(t), len(t3)+len(t7))
test_eq(len(get_files(path/'train'/'3', extensions='.jpg', recurse=False)),0)
test_eq(len(t), len(get_files(path, extensions='.png', recurse=True, folders='train')))
t

This behaviour of get_files also cascades to get_image_files (which depends of get_files).

This seems a bug to me, but I wanted to check it here before posting an issue at the Github repo.

I couldn’t recreate the error but I think the issue is that Path cannot take ~ notation, so I guess linkpath.ls() will return a FileNotFound error. It should be linkpath = Path("/home/fredguth/datasets").
I ran into that regularly and made it a habbit to always hit tab completion when creating a Path object :laughing:
Hope that solves it!

1 Like

In Python, ~ (tilde expansion) is not automatic. You can use $HOME or expanduser from the Python Standard Library to set your Home directory.
https://docs.python.org/3/library/os.path.html#os.path.expanduser

2 Likes

Thanks @benkarr and @willingc :slight_smile: it didn’t occurred to me that ~ was not valid.
/home/username/symbolic-link works.

2 Likes