Home > database >  How to get directory's properties on macOS with Python?
How to get directory's properties on macOS with Python?

Time:10-14

As a part of my Python project, I need to gather info about a specific folder (Date Edited, Date Created, size, etc.). Is there a particular library to do this on MacOS? Thanks!

CodePudding user response:

I would recommend using glob for making consistent path navigation (basically allows you to use the same notation across different operating systems), and using os for getting the attributes of each folder / file like so.

import glob
import os
dir_name = '/your/path/here'
# Get a list of files (file paths) in the given directory 
list_of_files = filter(os.path.isfile,
                    glob.glob(dir_name   '*') )
# get list of ffiles with size
files_with_size = [ (file_path, os.stat(file_path).st_size) 
                for file_path in list_of_files ]
# Iterate over list of tuples i.e. file_paths with size
# and print them one by one
for file_path, file_size in files_with_size:
    print(file_size, ' -->', file_path) 

CodePudding user response:

yea i'm not sure about whole directories, but you can os.walk() a given directory and pass them through os.stat()

here's a site with an overview of using that: https://flaviocopes.com/python-get-file-details/

  • Related