[英]How can you get MAC times/file attributes from all files in directory on Mac OS
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.我试图找出一种方法来获取过去 7 天目录中所有文件的所有 MAC 时间和其他文件属性。
I have tried variations of the "Find command multiple options | xargs stat -x", but still not getting what I am looking for.我尝试了“查找命令多个选项 | xargs stat -x”的变体,但仍然没有得到我想要的东西。
The shell command you need is stat -x *
.您需要的 shell 命令是
stat -x *
。 Here's how you can run that in Python:以下是在 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
.这将为您提供一个巨大的字符串,其中包含来自
stat
的所有 output 。 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
:然后你会在
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)'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.