Advertisement
calfred2808

PYTHON file type dir detector

Sep 9th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1.  
  2. #detect all with the file extension of jpg and png(file type dir detector)
  3. import os
  4.  
  5. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  6. image_dir = os.path.join(BASE_DIR, "images")
  7.  
  8. #D:\main folder\software\PROGRAMMING\Python\Python Sample program\Images
  9.  
  10. for root, dirs, files in os.walk(image_dir):
  11.     for file in files:
  12.         if file.endswith("jpg") or file.endswith("png"):
  13.             path = os.path.join(root, file)
  14.             label = os.path.basename(os.path.dirname(path)).replace(" ", "-").lower()
  15.  
  16.             print(label, path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement