I am trying to figure out a way to get all MAC times and other file attributes for all files in a directory for the past 7 days.
I have tried variations of the "Find command multiple options | xargs stat -x", but still not getting what I am looking for.
The shell command you need is stat -x *
. Here's how you can run that in Python:
import subprocess
filesPath = "/path/to/folder/*" # use /*.* instead to capture files only, no directories
allData = subprocess.run("stat -x " + filesPath, shell=True, capture_output=True,
universal_newlines=True).stdout
That will give you one giant string with all of the output from stat
. If you want to format that into a nice list of dictionaries for each file, you can use this:
import re
files = [("File: " + f).strip() for f in allData.split("File: ")]
fileDictList = [dict(re.findall(r"(\w+): (.*?)\s*(?=\w+: |$)", fileData)) for fileData in files]
Then you'll end up with something like the following in fileDictList
:
[{'Access': 'Tue Jul 7 19:02:47 2020',
'Change': 'Tue Jul 7 19:02:46 2020',
'Device': '1,4',
'File': '"/Users/bob/Documents/a.png"',
'FileType': 'Regular File',
'Gid': '( 20/ staff)',
'Inode': '6754586',
'Links': '1',
'Mode': '(0644/-rw-r--r--)',
'Modify': 'Wed Jun 17 01:36:53 2020',
'Size': '620729',
'Uid': '( 501/bob)'},
{'Access': 'Tue Jul 7 19:02:47 2020',
'Change': 'Tue Jul 7 19:02:46 2020',
'Device': '1,4',
'File': '"/Users/bob/Documents/b.png"',
'FileType': 'Regular File',
'Gid': '( 20/ staff)',
'Inode': '6754585',
'Links': '1',
'Mode': '(0644/-rw-r--r--)',
'Modify': 'Wed Jun 17 01:36:52 2020',
'Size': '839719',
'Uid': '( 501/bob)'}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.