Python實(shí)現(xiàn)批量修改文件時(shí)間屬性
前言
有時(shí)候需要修改文件的“修改時(shí)間” 、 “訪問時(shí)間”,“創(chuàng)建時(shí)間” 使用 Python 寫出來簡(jiǎn)單好用。
探索
讀取文件的屬性時(shí)間
import os
import time
# 獲取文件的基本屬性
def get_data(file_path, change):
# 文件創(chuàng)建時(shí)間
create_time = os.path.getctime(file_path)
create_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))
# 文件的修改時(shí)間
modification_time = os.path.getmtime(file_path)
modification_time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modification_time))
# 文件的訪問時(shí)間
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)
更改文件屬性時(shí)間
import os
import time
def change_time(file_path):
now = time.time() # 獲取時(shí)間戳
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) # 當(dāng)前時(shí)間
os.utime(file_path, (now, now))
注意:這里無法修改創(chuàng)建時(shí)間,只能走另一種方法:
使用 win32file 修改時(shí)間屬性
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)建時(shí)間
modifyTime = "2019-02-02 00:01:03" # 修改時(shí)間
accessTime = "2019-02-02 00:01:04" # 訪問時(shí)間
# 修改文件時(shí)間
def modifyFileTime(filePath ):
try:
format_str = "%Y-%m-%d %H:%M:%S" # 時(shí)間格式
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)建時(shí)間
modifyTime = "2019-02-02 00:01:03" # 修改時(shí)間
accessTime = "2019-02-02 00:01:04" # 訪問時(shí)間
# 修改文件時(shí)間
def modifyFileTime(filePath ):
try:
format_str = "%Y-%m-%d %H:%M:%S" # 時(shí)間格式
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負(fù)責(zé)記錄深度;
def deepDir(filepath,flag=0):
global filecount
global dircount
filepath+="/"
file_list = os.listdir(filepath)
flag+=2
# 負(fù)責(zé)存放目錄名稱
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}!')到此這篇關(guān)于Python實(shí)現(xiàn)批量修改文件時(shí)間屬性的文章就介紹到這了,更多相關(guān)Python修改文件時(shí)間屬性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python安裝mysql的依賴包mysql-python操作
這篇文章主要介紹了python安裝mysql的依賴包mysql-python操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
python基于雙向鏈表實(shí)現(xiàn)LFU算法
這篇文章主要為大家詳細(xì)介紹了python基于雙向鏈表實(shí)現(xiàn)LFU算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Matlab、Python為工具解析數(shù)據(jù)可視化之美
下面介紹一些數(shù)據(jù)可視化的作品(包含部分代碼),主要是地學(xué)領(lǐng)域,可遷移至其他學(xué)科,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11
python中強(qiáng)大的format函數(shù)實(shí)例詳解
python中format函數(shù)用于字符串的格式化,這篇文章主要介紹了python中強(qiáng)大的format函數(shù),需要的朋友可以參考下2018-12-12
Python實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python實(shí)現(xiàn)矩陣轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Pytorch框架實(shí)現(xiàn)mnist手寫庫(kù)識(shí)別(與tensorflow對(duì)比)
這篇文章主要介紹了Pytorch框架實(shí)現(xiàn)mnist手寫庫(kù)識(shí)別(與tensorflow對(duì)比),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

