Python修改文件的“修改时间”
程序员文章站
2022-05-24 16:12:02
...
import os
import random
import time
import datetime
# import xlwt
import xlrd
def ModifyFileTime1(file_path,timestr):
strFormat = "%Y-%m-%d %H:%M:%S.%f"
datetime_obj = time.mktime(time.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f"))
stinfo = os.stat(file_path)
# print("a2.py 的访问时间: %s" % stinfo.st_atime)
# print("a2.py 的修改时间: %s" % stinfo.st_mtime)
os.utime(file_path, (stinfo.st_mtime, datetime_obj))
if __name__ == "__main__":
#读配置,重新写配置
#ReadXlsx(mConfigFile)
#读取新配置,修改文件的操作时间
# SetFileOpTime(mPathFile)
g_filepath = r'E:\temp\Test修改文件时间.txt'
timestr = '2021-01-01 15:22:18.001'
try:
ModifyFileTime1(g_filepath, timestr)
print(g_filepath, timestr)
print("修改时间成功")
except:
print(g_filepath, timestr)
print("异常")