-2

I need to find all the path of some images .jpg but i can´t find them.

You can look at this image where I have my files .jpg: path of images

and I am using this python code:

session_path = "content/drive/My Drive/face"
import os
import re
from glob import glob
result = [y for x in os.walk(session_path) for y in glob(os.path.join(x[0], '*.jpg'))]
result

but result is an empty array: []

noraj
  • 3,964
  • 1
  • 30
  • 38
  • Hi, what do you mean by " I need to find all the path of some images" ? To make your question clearer please take a look at: https://stackoverflow.com/help/how-to-ask – noraj May 18 '19 at 16:59
  • Have a look: https://stackoverflow.com/questions/2909975/python-list-directory-subdirectory-and-files – Peter May 18 '19 at 17:08

1 Answers1

1
session_path = 'content/drive/My Drive/face'
jpegs = []
for root, dirs, files in os.walk(session_path):
    for file in files:
        if file.endswith('.jpg'):
            jpegs.append(os.path.join(root, file))    
J_H
  • 17,926
  • 4
  • 24
  • 44
  • Some Improvements:- 1) Your could pass in `file.endswith()` a tuple `(".jpeg",".jpg")` which allows it to be a lot more flexible while dealing with jpeg family images. 2) I would not recommend you to use `file.endswith()` in order to determine the extension of the filename, coz it treats the filename as a string, which allows certain file's to be selected, which won't be selected otherwise. Like extension less files having `.jpg` in their filename . ex.`far.jpg` where the `.jpg` is the filename rather then the ext. Rather use `os.path.splitext()[1]` for obtaining actual extension of a file. – Vasu Deo.S May 19 '19 at 00:24