![](/img/trans.png)
[英]Mac OS; Python3; FileNotFoundError: [Errno 2] No such file or directory: ''. How to fix it?
[英]How can you get MAC times/file attributes from all files in directory on Mac OS
我試圖找出一種方法來獲取過去 7 天目錄中所有文件的所有 MAC 時間和其他文件屬性。
我嘗試了“查找命令多個選項 | 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
的所有 output 。 如果要將其格式化為每個文件的不錯的字典列表,可以使用以下命令:
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)'}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.