Use os.path.join()
inside glob.glob()
. Also, if all your images are of a particular extension (say, jpg), you could replace '*.*'
with '*.jpg*'
for example.
Solution
import os, glob
files = glob.glob(os.path.join(path,'train/*.*'))
As a matter of fact, you might as well just do the following using os
library alone, since you are not selecting any particular file extension type.
import os
files = os.listdir(os.path.join(path,'train'))
Some Explanation
The method os.path.join()
here helps you join multiple folders together to create a path. This will work whether you are on a Windows/Mac/Linux system. But, for windows the path-separator is \
and for Mac/Linux it is /
. So, not using os.path.join()
could create an un-resolvable path for the OS. I would use glob.glob
when I am interested in getting some specific types (extensions) of files. But glob.glob(path)
requires a valid path to work with. In my solution, os.path.join()
is creating that path from the path components and feeding it into glob.glob()
.
For more clarity, I suggest you see documentation for os.path.join
and glob.glob
.
Also, see pathlib
module for path manipulation as an alternative to os.path.join()
.