python如何修改文件時(shí)間屬性
# -*- encoding=utf-8 -*-import osimport timedef get_file_time(filename): filename = os.path.abspath(filename) create_time = os.path.getctime(filename) # 創(chuàng)建時(shí)間 print(’old create time:{}’.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(create_time)))) update_time = os.path.getmtime(filename) # 修改時(shí)間 print(’old update time:{}’.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(update_time)))) access_time = os.path.getatime(filename) # 訪問時(shí)間 print(’old access time:{}’.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(access_time)))) return create_time, update_time, access_timeif __name__ == ’__main__’: get_file_time(’E:/a.txt’)
# -*- encoding=utf-8 -*-import osimport timedef set_file_time(filename, updatetime, access_time): # 先傳修改時(shí)間,再傳訪問時(shí)間 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 osimport timedef get_file_time(filename): filename = os.path.abspath(filename) # 創(chuàng)建時(shí)間 create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(filename))) # 修改時(shí)間 update_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(filename))) # 訪問時(shí)間 access_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getatime(filename))) return create_time, update_time, access_timedef set_file_time(filename, updatetime, access_time): # 先傳修改時(shí)間,再傳訪問時(shí)間 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、補(bǔ)充修改文件的創(chuàng)建時(shí)間
import osimport timefrom pywintypes import Time # 可以忽視這個(gè) Time 報(bào)錯(cuò)(運(yùn)行程序還是沒問題的)from win32con import FILE_FLAG_BACKUP_SEMANTICSfrom win32con import FILE_SHARE_WRITEfrom win32file import CloseHandlefrom win32file import CreateFilefrom win32file import GENERIC_WRITEfrom win32file import OPEN_EXISTINGfrom win32file import SetFileTimedef modify_file_create_time(filename, create_time_str, update_time_str, access_time_str): try: format_str = '%Y-%m-%d %H:%M:%S' # 時(shí)間格式 # 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' # 創(chuàng)建時(shí)間 mTime = '2019-02-02 00:01:03' # 修改時(shí)間 aTime = '2019-02-02 00:01:04' # 訪問時(shí)間 fName = r'a.txt' # 可以是文件也可以是文件夾 print(os.path.isdir(fName)) modify_file_create_time(fName, cTime, mTime, aTime)
以上就是python如何修改文件時(shí)間屬性的詳細(xì)內(nèi)容,更多關(guān)于python修改文件時(shí)間屬性的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python b站視頻下載的五種版本2. 如何通過vscode運(yùn)行調(diào)試javascript代碼3. 半小時(shí)實(shí)現(xiàn)Java手?jǐn)]網(wǎng)絡(luò)爬蟲框架(附完整源碼)4. 測試模式 - XSL教程 - 55. 教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)6. JAVA抽象類及接口使用方法解析7. python如何寫個(gè)俄羅斯方塊8. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)9. Python結(jié)合百度語音識(shí)別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)10. JavaScript設(shè)計(jì)模式之策略模式實(shí)現(xiàn)原理詳解
![半小時(shí)實(shí)現(xiàn)Java手?jǐn)]網(wǎng)絡(luò)爬蟲框架(附完整源碼)](http://www.aoyou183.cn/attached/image/14.jpg)