0

I'm trying to print out the filename of files in my folder but unfortunately when I print it out it comes out as - Image is ././Image_crop 4.jpg - how do I get rid of the ././ in front of the file? I'm new to python so please help!

for filename in glob.glob(os.path.join(".", '*.jpg')):
    y = os.path.join(".", filename)
    print "Image is ", y
Abid Abdul Gafoor
  • 462
  • 1
  • 6
  • 18
  • Change `y = os.path.join(".", filename)` to `y = filename`? And if you want to get rid of both, also change `os.path.join(".", '*.jpg')` to `'*.jpg'`? – Aran-Fey Jun 11 '18 at 10:58
  • 1
    If you just want the actual filename: Have look at [os.path.basename](https://docs.python.org/2/library/os.path.html#os.path.basename) - But why are you joining the "." in the first place? – Sebastian Loehner Jun 11 '18 at 10:59
  • You should just stop to put the `./` in front of the actual glob. You put one in in the first line and one in the second. – Klaus D. Jun 11 '18 at 11:36

1 Answers1

1

Try this :

print os.path.basename(filename)

It will remove the path and give you the actual filename only. See Extract file name from path, no matter what the os/path format for more info.

Chuk Ultima
  • 987
  • 1
  • 11
  • 21