python获取文件创建时间、修改时间.md 601 B

#python

代码:

import os

import time

# 获取文件的创建时间和修改时间

file_path = 'test.txt'

create_time = os.path.getctime(file_path)

modify_time = os.path.getmtime(file_path)

# 转换为本地时间格式

create_time_local = time.localtime(create_time)

modify_time_local = time.localtime(modify_time)

# 打印结果

print('文件创建时间:', time.strftime('%Y-%m-%d %H:%M:%S', create_time_local))

print('文件修改时间:', time.strftime('%Y-%m-%d %H:%M:%S', modify_time_local))

根据具体需求,定制即可

![[Pasted image 20240424153844.png]]