如何从MAC OS上目录中的所有文件中获取MAC时间/文件属性



我正在想办法获取过去7天目录中所有文件的所有MAC时间和其他文件属性。

我尝试过";Find命令多个选项|xargs stat-x";,但仍然没有得到我想要的。

您需要的shell命令是stat -x *。以下是如何在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

这将为您提供一个巨大的字符串,其中包含stat的所有输出。如果你想将其格式化为每个文件的字典列表,你可以使用以下方法:

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]

然后,您将在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)'}]

最新更新