最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)批量修改文件時間屬性

 更新時間:2023年11月09日 09:25:08   作者:戀戀西風(fēng)  
我們有時候需要修改文件的“修改時間”?、?“訪問時間”,“創(chuàng)建時間”?,此時如果使用Python批量實現(xiàn)應(yīng)該會方便很多,下面小編就來為大家介紹一下具體實現(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))  # 當(dāng)前時間
 
    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負(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實現(xiàn)批量修改文件時間屬性的文章就介紹到這了,更多相關(guān)Python修改文件時間屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中int與str互轉(zhuǎn)方法

    python中int與str互轉(zhuǎn)方法

    最近學(xué)習(xí)python中的數(shù)據(jù)類型時,難免聯(lián)想到j(luò)ava中的基本型數(shù)據(jù)類型與引用型數(shù)據(jù)類型。接下來通過本文給大家介紹python中int與str互轉(zhuǎn),需要的朋友可以參考下
    2018-07-07
  • python實現(xiàn)dijkstra最短路由算法

    python實現(xiàn)dijkstra最短路由算法

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)dijkstra最短路由算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python中@Property屬性使用方法

    python中@Property屬性使用方法

    這篇文章主要介紹了python中@Property屬性使用方法,在Python中,可以通過@property裝飾器將一個方法轉(zhuǎn)換為屬性,從而實現(xiàn)用于計算的屬性,下面文章圍繞主題展開更多相關(guān)詳情,感興趣的小伙伴可以參考一下
    2022-06-06
  • tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳解

    tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳

    這篇文章主要介紹了tensorflow dataset.shuffle、dataset.batch、dataset.repeat順序區(qū)別詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2020-06-06
  • 詳解如何將Python可執(zhí)行文件(.exe)反編譯為Python腳本

    詳解如何將Python可執(zhí)行文件(.exe)反編譯為Python腳本

    將?Python?可執(zhí)行文件(.exe)反編譯為?Python?腳本是一項有趣的技術(shù)挑戰(zhàn),可以幫助我們理解程序的工作原理,下面我們就來看看具體實現(xiàn)步驟吧
    2024-03-03
  • Python開發(fā)中OpenCV視頻流的多線程處理方式詳解

    Python開發(fā)中OpenCV視頻流的多線程處理方式詳解

    在做視覺類項目中,常常需要在Python環(huán)境下使用OpenCV讀取本地的還是網(wǎng)絡(luò)攝像頭的視頻流,之后再調(diào)入各種模型,所以本文我們就來聊聊OpenCV視頻流的多線程處理方式吧
    2025-05-05
  • Python實現(xiàn)將json格式數(shù)據(jù)存儲到Mysql數(shù)據(jù)庫

    Python實現(xiàn)將json格式數(shù)據(jù)存儲到Mysql數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實現(xiàn)將json格式數(shù)據(jù)存儲到Mysql數(shù)據(jù)庫,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考下
    2025-03-03
  • 解決Python運行文件出現(xiàn)out of memory框的問題

    解決Python運行文件出現(xiàn)out of memory框的問題

    今天小編就為大家分享一篇解決Python運行文件出現(xiàn)out of memory框的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python 布爾類型示例精講

    Python 布爾類型示例精講

    這篇文章主要為大家介紹了Python 布爾類型示例精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Python TestCase中的斷言方法介紹

    Python TestCase中的斷言方法介紹

    這篇文章主要給大家介紹了關(guān)于Python TestCase中的斷言方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評論

磐安县| 蓬溪县| 广宁县| 布拖县| 金沙县| 平塘县| 大埔区| 洪雅县| 巴南区| 普格县| 万山特区| 黄山市| 元朗区| 祁东县| 甘德县| 肇州县| 拉萨市| 综艺| 梁平县| 彰化县| 辽阳县| 汨罗市| 扶风县| 郎溪县| 衡阳县| 乌兰浩特市| 嘉黎县| 台中县| 四子王旗| 顺昌县| 黔西| 洪湖市| 高清| 白城市| 辰溪县| 克什克腾旗| 南充市| 烟台市| 班玛县| 太原市| 金沙县|