Python Forum
Listing directories (as a text file)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listing directories (as a text file)
#1
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)
buran write Feb-17-2023, 06:45 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
to list image attributes see: https://pythontic.com/image-processing/p...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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 439 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 718 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Find duplicate files in multiple directories Pavel_47 9 3,144 Dec-27-2022, 04:47 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk
  Read directory listing of files and parse out the highest number? cubangt 5 2,374 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,695 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,017 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  I need to copy all the directories that do not match the pattern tester_V 7 2,447 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Functions to consider for file renaming and moving around directories cubangt 2 1,767 Jan-07-2022, 02:16 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020