python 兩種方法修改文件的創(chuàng)建時(shí)間、修改時(shí)間、訪問時(shí)間
突如其來想知道一下 python 如何修改文件的屬性(創(chuàng)建、修改、訪問時(shí)間),于是就去網(wǎng)上搜集了可行方案,也就有了這篇博客
方案一
from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandle
from win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING
from pywintypes import Time # 可以忽視這個(gè) Time 報(bào)錯(cuò)(運(yùn)行程序還是沒問題的)
import time
def modifyFileTime(filePath, createTime, modifyTime, accessTime, offset):
"""
用來修改任意文件的相關(guān)時(shí)間屬性,時(shí)間格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02
:param filePath: 文件路徑名
:param createTime: 創(chuàng)建時(shí)間
:param modifyTime: 修改時(shí)間
:param accessTime: 訪問時(shí)間
:param offset: 時(shí)間偏移的秒數(shù),tuple格式,順序和參數(shù)時(shí)間對應(yīng)
"""
try:
format = "%Y-%m-%d %H:%M:%S" # 時(shí)間格式
cTime_t = timeOffsetAndStruct(createTime, format, offset[0])
mTime_t = timeOffsetAndStruct(modifyTime, format, offset[1])
aTime_t = timeOffsetAndStruct(accessTime, format, offset[2])
fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
createTimes, accessTimes, modifyTimes = GetFileTime(fh)
createTimes = Time(time.mktime(cTime_t))
accessTimes = Time(time.mktime(aTime_t))
modifyTimes = Time(time.mktime(mTime_t))
SetFileTime(fh, createTimes, accessTimes, modifyTimes)
CloseHandle(fh)
return 0
except:
return 1
def timeOffsetAndStruct(times, format, offset):
return time.localtime(time.mktime(time.strptime(times, format)) + offset)
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"E:\test_pro\fileOperate\test.xlsx" # 文件路徑,文件存在才能成功(可以寫絕對路徑,也可以寫相對路徑)
offset = (0, 1, 2) # 偏移的秒數(shù)(不知道干啥的)
# 調(diào)用函數(shù)修改文件創(chuàng)建時(shí)間,并判斷是否修改成功
r = modifyFileTime(fName, cTime, mTime, aTime, offset)
if r == 0:
print('修改完成')
elif r == 1:
print('修改失敗')
方案二(無法修改文件創(chuàng)建時(shí)間)
可以去這里http://tools.jb51.net/code/unixtime/轉(zhuǎn)換時(shí)間,也可以自己處理時(shí)間戳與格式化時(shí)間
import os file_path = "pip.txt" print(os.stat(file_path)) # os.stat_result( # st_mode=33206, # st_ino=2251799813766228, # st_dev=3050226722, # st_nlink=1, # st_uid=0, # st_gid=0, # st_size=851, # st_atime=1576241919, # st_mtime=1574385498, # st_ctime=1576241919, # ) # 只能修改 訪問時(shí)間 與 修改時(shí)間(暫不知道怎么修改創(chuàng)建時(shí)間) os.utime(file_path, (1576335480, 1576335480)) print(os.stat(file_path)) # os.stat_result( # st_mode=33206, # st_ino=2251799813766228, # st_dev=3050226722, # st_nlink=1, # st_uid=0, # st_gid=0, # st_size=851, # st_atime=1576335480, # st_mtime=1576335480, # st_ctime=1576241919, # )
以上就是python 兩種方法修改文件的創(chuàng)建時(shí)間、修改時(shí)間、訪問時(shí)間的詳細(xì)內(nèi)容,更多關(guān)于python 修改文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何讓PyQt5中QWebEngineView與JavaScript交互
這篇文章主要介紹了如何讓PyQt5中QWebEngineView與JavaScript交互,幫助大家更好的理解和學(xué)習(xí)PyQt5框架,感興趣的朋友可以了解下2020-10-10
Python?Haul利器簡化數(shù)據(jù)爬取任務(wù)提高開發(fā)效率
Haul?是一個(gè)專門為數(shù)據(jù)爬取任務(wù)而設(shè)計(jì)的?Python?庫,它提供了一系列的工具和功能,幫助我們輕松處理數(shù)據(jù)爬取中的重復(fù)工作和復(fù)雜問題2024-01-01
Python讀取文件內(nèi)容為字符串的方法(多種方法詳解)
這篇文章主要介紹了Python讀取文件內(nèi)容為字符串的方法,本文通過三種方式給大家介紹,在文章末尾給大家提到了python讀取txt文件中字符串,字符串用空格分隔的相關(guān)知識(shí),需要的朋友可以參考下2020-03-03
使用Python將圖片轉(zhuǎn)正方形的兩種方法實(shí)例代碼詳解
這篇文章主要介紹了使用Python將圖片轉(zhuǎn)正方形的兩種方法,本文通過實(shí)例代碼給大家給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python&Matlab實(shí)現(xiàn)灰狼優(yōu)化算法的示例代碼
灰狼優(yōu)化算法是一種群智能優(yōu)化算法,它的獨(dú)特之處在于一小部分擁有絕對話語權(quán)的灰狼帶領(lǐng)一群灰狼向獵物前進(jìn)。本文具體介紹了灰狼優(yōu)化算法的兩種實(shí)現(xiàn)示例代碼,需要的可以參考一下2022-03-03
聊聊Numpy.array中[:]和[::]的區(qū)別在哪
這篇文章主要介紹了在Numpy.array中[:]和[::]的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python中slice參數(shù)過長的處理方法及實(shí)例
在本篇文章里小編給大家分享了一篇關(guān)于python中slice參數(shù)過長的處理方法及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2020-12-12

