1、获取文件的创建、修改、访问时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# -*- encoding=utf-8 -*- import os import time def get_file_time(filename): filename = os.path.abspath(filename) create_time = os.path.getctime(filename) # 创建时间 print ( 'old create time:{}' . format (time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(create_time)))) update_time = os.path.getmtime(filename) # 修改时间 print ( 'old update time:{}' . format (time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(update_time)))) access_time = os.path.getatime(filename) # 访问时间 print ( 'old access time:{}' . format (time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(access_time)))) return create_time, update_time, access_time if __name__ = = '__main__' : get_file_time( 'e:/a.txt' ) |
2、更改文件的修改、访问时间(创建时间没查到怎么修改,暂时不记录)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# -*- encoding=utf-8 -*- import os import time def set_file_time(filename, updatetime, access_time): # 先传修改时间,再传访问时间 filename = os.path.abspath(filename) new_updatetime = time.mktime(time.strptime(updatetime, '%y-%m-%d %h:%m:%s' )) new_access_time = time.mktime(time.strptime(access_time, '%y-%m-%d %h:%m:%s' )) os.utime(filename, (new_access_time, new_updatetime)) if __name__ = = '__main__' : set_file_time( 'e:/a.txt' , '2018-01-08 10:50:20' , '2019-07-15 04:03:01' ) |
3、放在同一个py方便直接复制使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# -*- encoding=utf-8 -*- import os import time def get_file_time(filename): filename = os.path.abspath(filename) # 创建时间 create_time = time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(os.path.getctime(filename))) # 修改时间 update_time = time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(os.path.getmtime(filename))) # 访问时间 access_time = time.strftime( "%y-%m-%d %h:%m:%s" , time.localtime(os.path.getatime(filename))) return create_time, update_time, access_time def set_file_time(filename, updatetime, access_time): # 先传修改时间,再传访问时间 filename = os.path.abspath(filename) new_update_time = time.mktime(time.strptime(updatetime, '%y-%m-%d %h:%m:%s' )) new_access_time = time.mktime(time.strptime(access_time, '%y-%m-%d %h:%m:%s' )) os.utime(filename, (new_access_time, new_update_time)) def debug(): create_time, update_time, access_time = get_file_time( 'e:/a.txt' ) set_file_time( 'e:/a.txt' , update_time, access_time) get_file_time( 'e:/a.txt' ) if __name__ = = '__main__' : debug() |
4、补充修改文件的创建时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import os import time from pywintypes import time # 可以忽视这个 time 报错(运行程序还是没问题的) from win32con import file_flag_backup_semantics from win32con import file_share_write from win32file import closehandle from win32file import createfile from win32file import generic_write from win32file import open_existing from win32file import setfiletime def modify_file_create_time(filename, create_time_str, update_time_str, access_time_str): try : format_str = "%y-%m-%d %h:%m:%s" # 时间格式 # f = createfile(filename, generic_read | generic_write, 0, none, open_existing, 0, 0) f = createfile(filename, generic_write, file_share_write, none, open_existing, file_flag_backup_semantics, 0 ) create_time = time(time.mktime(time.strptime(create_time_str, format_str))) update_time = time(time.mktime(time.strptime(update_time_str, format_str))) access_time = time(time.mktime(time.strptime(access_time_str, format_str))) setfiletime(f, create_time, update_time, access_time) closehandle(f) print ( 'update file time success:{}/{}/{}' . format (create_time_str, update_time_str, access_time_str)) except exception as e: print ( 'update file time fail:{}' . format (e)) if __name__ = = '__main__' : ctime = "2019-12-13 21:51:02" # 创建时间 mtime = "2019-02-02 00:01:03" # 修改时间 atime = "2019-02-02 00:01:04" # 访问时间 fname = r "a.txt" # 可以是文件也可以是文件夹 print (os.path.isdir(fname)) modify_file_create_time(fname, ctime, mtime, atime) |
以上就是python如何修改文件时间属性的详细内容,更多关于python修改文件时间属性的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/likeatree/p/4280388.html