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

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

 更新時(shí)間:2023年11月09日 09:25:08   作者:戀戀西風(fēng)  
我們有時(shí)候需要修改文件的“修改時(shí)間”?、?“訪問時(shí)間”,“創(chuàng)建時(shí)間”?,此時(shí)如果使用Python批量實(shí)現(xiàn)應(yīng)該會(huì)方便很多,下面小編就來為大家介紹一下具體實(shí)現(xiàn)方法吧

前言

有時(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中int與str互轉(zhuǎn)方法

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

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

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

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

    python中@Property屬性使用方法

    這篇文章主要介紹了python中@Property屬性使用方法,在Python中,可以通過@property裝飾器將一個(gè)方法轉(zhuǎn)換為屬性,從而實(shí)現(xiàn)用于計(jì)算的屬性,下面文章圍繞主題展開更多相關(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ū)別詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2020-06-06
  • 詳解如何將Python可執(zhí)行文件(.exe)反編譯為Python腳本

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

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

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

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

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

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

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

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

    Python 布爾類型示例精講

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

    Python TestCase中的斷言方法介紹

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

最新評(píng)論

麻阳| 依安县| 乳山市| 耒阳市| 岐山县| 原平市| 保德县| 广饶县| 龙南县| 双峰县| 冷水江市| 阿坝| 大城县| 扎鲁特旗| 怀来县| 江川县| 巴彦淖尔市| 博野县| 汉阴县| 老河口市| 景东| 霍邱县| 固安县| 蒙城县| 仪陇县| 塔城市| 通山县| 大方县| 科尔| 兴仁县| 鹿泉市| 铜鼓县| 亚东县| 海口市| 广河县| 九龙城区| 福州市| 神农架林区| 阳原县| 新巴尔虎右旗| 卢氏县|