Python實現(xiàn)批量修改文件時間屬性
更新時間:2023年11月09日 09:25:08 作者:戀戀西風
我們有時候需要修改文件的“修改時間”?、?“訪問時間”,“創(chuàng)建時間”?,此時如果使用Python批量實現(xiàn)應該會方便很多,下面小編就來為大家介紹一下具體實現(xiàn)方法吧
前言
有時候需要修改文件的“修改時間” 、 “訪問時間”,“創(chuàng)建時間” 使用 Python 寫出來簡單好用。
探索
讀取文件的屬性時間
import os import time # 獲取文件的基本屬性 def get_data(file_path, change): # 文件創(chuàng)建時間 create_time = os.path.getctime(file_path) create_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time)) # 文件的修改時間 modification_time = os.path.getmtime(file_path) modification_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modification_time)) # 文件的訪問時間 access_time = os.path.getatime(file_path) access_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time)) table.add_row(create_time1, modification_time1, access_time1, change)
更改文件屬性時間
import os import time def change_time(file_path): now = time.time() # 獲取時間戳 localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) # 當前時間 os.utime(file_path, (now, now))
注意:這里無法修改創(chuàng)建時間,只能走另一種方法:
使用 win32file 修改時間屬性
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 createTime = "2019-12-13 21:51:02" # 創(chuàng)建時間 modifyTime = "2019-02-02 00:01:03" # 修改時間 accessTime = "2019-02-02 00:01:04" # 訪問時間 # 修改文件時間 def modifyFileTime(filePath ): try: format_str = "%Y-%m-%d %H:%M:%S" # 時間格式 f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) create_time = datetime.datetime.strptime(createTime, format_str) update_time = datetime.datetime.strptime(modifyTime, format_str) access_time = datetime.datetime.strptime(accessTime, format_str) SetFileTime(f, create_time, update_time, access_time) CloseHandle(f) return True except Exception as e: print(e) return False
完整代碼
import os import time import datetime import win32timezone 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 createTime = "2019-12-13 21:51:02" # 創(chuàng)建時間 modifyTime = "2019-02-02 00:01:03" # 修改時間 accessTime = "2019-02-02 00:01:04" # 訪問時間 # 修改文件時間 def modifyFileTime(filePath ): try: format_str = "%Y-%m-%d %H:%M:%S" # 時間格式 f = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) create_time = datetime.datetime.strptime(createTime, format_str) update_time = datetime.datetime.strptime(modifyTime, format_str) access_time = datetime.datetime.strptime(accessTime, format_str) SetFileTime(f, create_time, update_time, access_time) CloseHandle(f) return True except Exception as e: print(e) return False dircount=0 filecount=0 # i負責記錄深度; def deepDir(filepath,flag=0): global filecount global dircount filepath+="/" file_list = os.listdir(filepath) flag+=2 # 負責存放目錄名稱 dirls=[] for tempfile in file_list: if os.path.isdir(filepath+"/"+tempfile): dirls.append(filepath+"/"+tempfile) else: filecount+=1 print('-'*flag,end='') print(tempfile) modifyFileTime(filepath+"/"+tempfile) for tempfile in dirls: dircount+=1 deepDir(tempfile,flag) if __name__=="__main__": # try: dir=input('please copy your dir and paste here (Be sure to copy directly):') deepDir(dir.replace('\\','/')) print(f'completed file nums is:{filecount} and dir num is {dircount}!')
到此這篇關于Python實現(xiàn)批量修改文件時間屬性的文章就介紹到這了,更多相關Python修改文件時間屬性內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python安裝mysql的依賴包mysql-python操作
這篇文章主要介紹了python安裝mysql的依賴包mysql-python操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Matlab、Python為工具解析數(shù)據可視化之美
下面介紹一些數(shù)據可視化的作品(包含部分代碼),主要是地學領域,可遷移至其他學科,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-11-11Pytorch框架實現(xiàn)mnist手寫庫識別(與tensorflow對比)
這篇文章主要介紹了Pytorch框架實現(xiàn)mnist手寫庫識別(與tensorflow對比),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07