这是我调用stat
命令并手动解析输出的解决方案。请随时发布不需要这样做的解决方案。
import datetime
import re
import subprocess
def stat_ns(path):
'''
Python doesn't support nanoseconds yet, so just return the total # of nanoseconds.
'''
# Use human-readable formats to include nanoseconds
outputs = subprocess.check_output(['stat', '--format=%x\n%y\n%z\n%w', path]).decode().strip().split('\n')
res = {}
for name, cur_output in zip(('access', 'modify', 'change', 'birth'), outputs):
# Remove the ns part and process it ourselves, as Python doesn't support it
start, mid, end = re.match(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\.(\d{9}) ([+-]\d{4})', cur_output).groups()
seconds = int(datetime.datetime.strptime(f'{start} {end}', '%Y-%m-%d %H:%M:%S %z').timestamp())
ns = 10**9 * seconds + int(mid)
res[name] = ns
return res
啊函数的示例输出如下所示:
{'access': 1583824344829877823,
'modify': 1583824346649884067,
'change': 1583824346649884067,
'birth': 1583813803975447216}