简体   繁体   English

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

[英]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.

相关问题 Mac 操作系统; 蟒蛇3; FileNotFoundError: [Errno 2] 没有这样的文件或目录:''。 如何解决? - Mac OS; Python3; FileNotFoundError: [Errno 2] No such file or directory: ''. How to fix it? 你能用 Python 创建一个 Mac OS X 服务吗? 如何? - Can you create an Mac OS X Service with Python? How? 如何在Mac上将目录从文件定向到另一个目录 - How to direct directory from file to another on Mac 如何在 Mac OS 上使用 Tkinter 获取黑色文件对话框? - How to get a black file dialog box using Tkinter on Mac OS? 无法显示图像,没有文件或目录(Mac) - Can't get images to show up, No file or directory(Mac) 如何在Mac OS X中从二进制文件创建.app文件? - How to create an .app file in mac os x from binaries? 如何在Mac OS X上的py2app打包的Python应用程序中获取系统默认的语言/语言环境? - How can you get the system default language/locale, in a py2app packaged Python app on Mac OS X? Mac OS X错误:PYTHONPATH上没有的目录以及哪些Python不读取“.pth”文件 - Mac OS X error: directory that is not on PYTHONPATH and which Python does not read “.pth” files from 如何获取文件路径目录并使用它来读取我的Excel文件? (苹果电脑) - How to get filepath directory and use it to read my excel file? (Mac) 如何通过终端获取Mac OS的语言 - How to get the language of mac os through terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM