您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用python怎么修改文件的時間屬性,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
# -*- 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')
# -*- 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')
# -*- 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()
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怎么修改文件的時間屬性就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。