Help on built-in function utime in module posix:
utime(...)
utime(path, (atime, mtime))
utime(path, None)
Set the access and modified time of the file to the given values. If the
second form is used, set the access and modified times to the current time.
In [141]: !touch test.txt
In [143]: os.stat('test.txt')
Out[143]: os.stat_result(st_mode=33206, st_ino=14355223812719680, st_dev=651824810, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1532879422, st_mtime=1532879422, st_ctime=1532879404)
In [146]: test_stat = os.stat('test.txt')
In [147]: test_stat.st_mtime
Out[147]: 1532879422.2283707
# 查看文件的修改时间
In [149]: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(test_stat.st_mtime))
Out[149]: '2018-07-29 23:50:22'
# 查看文件的上次访问时间
In [150]: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(test_stat.st_atime))
Out[150]: '2018-07-29 23:50:22'
# 设置新的修改时间,并转为时间戳
In [153]: test_new_mtime = time.mktime(time.strptime('2017-07-29 23:50:22', '%Y-%m-%d %H:%
...: M:%S'))
# 设置新的上次访问时间,并转为时间戳
In [154]: test_new_atime = time.mktime(time.strptime('2016-07-30 23:50:22', '%Y-%m-%d %H:%
...: M:%S'))
# 修改上次访问时间、修改时间
In [155]: os.utime('test.txt', (test_new_atime, test_new_mtime))
# 获取修改后的文件属性
In [156]: new_test_stat = os.stat('test.txt')
# 查看修改后的修改时间
In [160]: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(new_test_stat.st_mtime))
Out[160]: '2017-07-29 23:50:22'
# 查看修改后的上次访问时间
In [161]: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(new_test_stat.st_atime))
Out[161]: '2016-07-30 23:50:22'