Python Forum
Listing directories (as a text file) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Listing directories (as a text file) (/thread-39435.html)



Listing directories (as a text file) - kiwi99 - Feb-17-2023

Is there a simple alternative to os.listdir that returns a text file of image sizes (height, width, MB) as well as document name?

I wondered if there was some enhanced version of os.listdir hidden in some Python Library?

I WOULD LIKE TO CREATE A TEXT FILE CONTAINING:
Document name, size in MB, Image width, image height in pixels

MY CODE SO FAR
I can get a text file containing all document names in a directory (see code below).

(It works but doesn't give enough info!)
gDir = "D:/family2022/"

# Now get a text list of the photos to check.
photosToCheck = "OriginalSourceForPhotos"
if os.path.exists(gDir + photosToCheck) :
	gListPhotoRecs = os.listdir(gDir + photosToCheck)
	print (gListPhotoRecs)
else:
	print ("Missing folder containing photos to check!")
	input ("Press ENTER")
Thanks in advance.
(Novice)


RE: Listing directories (as a text file) - Larz60+ - Feb-17-2023

to list image attributes see: https://pythontic.com/image-processing/pillow/attributes
to get list of images in a directory:
from pathlib import Path

dirname = Path('path to image directory')

# in next linereplace .suffix with actual image suffix, like .jpg, .bmp .tiff etc.
image_files = [img for img in dirname.iterdir() if img.is_file() and img.suffix == '.suffix' ]

for file in image_files:
    print(f"{file.name}") # or file.resolve() for full path