欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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高級(jí)語法之閉包和裝飾器詳解

    python高級(jí)語法之閉包和裝飾器詳解

    這篇文章主要介紹了python高級(jí)語法之閉包和裝飾器詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Python安裝selenium包詳細(xì)過程

    Python安裝selenium包詳細(xì)過程

    在本篇文章里小編給大家整理了關(guān)于Python安裝selenium包詳細(xì)過程,需要的朋友們可以學(xué)習(xí)下。
    2019-07-07
  • python安裝mysql的依賴包mysql-python操作

    python安裝mysql的依賴包mysql-python操作

    這篇文章主要介紹了python安裝mysql的依賴包mysql-python操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • python基于雙向鏈表實(shí)現(xiàn)LFU算法

    python基于雙向鏈表實(shí)現(xiàn)LFU算法

    這篇文章主要為大家詳細(xì)介紹了python基于雙向鏈表實(shí)現(xiàn)LFU算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python ssh 執(zhí)行shell命令的示例

    python ssh 執(zhí)行shell命令的示例

    這篇文章主要介紹了python ssh 執(zhí)行shell命令的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Matlab、Python為工具解析數(shù)據(jù)可視化之美

    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中強(qiáng)大的format函數(shù)實(shí)例詳解

    python中format函數(shù)用于字符串的格式化,這篇文章主要介紹了python中強(qiáng)大的format函數(shù),需要的朋友可以參考下
    2018-12-12
  • pytorch自定義loss損失函數(shù)

    pytorch自定義loss損失函數(shù)

    這篇文章主要介紹了pytorch自定義loss損失函數(shù),自定義loss的方法有很多,本文要介紹的是把loss作為一個(gè)pytorch的模塊,下面詳細(xì)資料需要的小伙伴可以參考一下
    2022-02-02
  • Python實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法分析

    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ì)比)

    這篇文章主要介紹了Pytorch框架實(shí)現(xiàn)mnist手寫庫(kù)識(shí)別(與tensorflow對(duì)比),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論