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!
Asked
Active
Viewed 54 times
-4
-
1Does this answer your question? [How do I get file creation and modification date/times?](https://stackoverflow.com/questions/237079/how-do-i-get-file-creation-and-modification-date-times) – mkrieger1 Oct 13 '22 at 16:16
-
I would recommend `pathlib` as a modern, OS-independent way of working with files and directories https://pymotw.com/3/pathlib/index.html – Mark Setchell Oct 13 '22 at 16:25
2 Answers
-1
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)

Allan Elder
- 4,052
- 17
- 19
-1
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/

Andrew Lien
- 102
- 4