亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python如何修改文件時(shí)間屬性

 更新時(shí)間:2021年02月05日 12:01:07   作者:withChengChen  
這篇文章主要介紹了python修改文件時(shí)間屬性的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

1、獲取文件的創(chuàng)建、修改、訪問(wèn)時(shí)間

# -*- encoding=utf-8 -*-
import os
import time


def get_file_time(filename):
  filename = os.path.abspath(filename)
  create_time = os.path.getctime(filename) # 創(chuàng)建時(shí)間
  print('old create time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))))
  update_time = os.path.getmtime(filename) # 修改時(shí)間
  print('old update time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_time))))
  access_time = os.path.getatime(filename) # 訪問(wèn)時(shí)間
  print('old access time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time))))
  return create_time, update_time, access_time


if __name__ == '__main__':
  get_file_time('E:/a.txt')

 2、更改文件的修改、訪問(wèn)時(shí)間(創(chuàng)建時(shí)間沒(méi)查到怎么修改,暫時(shí)不記錄)

# -*- encoding=utf-8 -*-
import os
import time

def set_file_time(filename, updatetime, access_time):
  # 先傳修改時(shí)間,再傳訪問(wèn)時(shí)間
  filename = os.path.abspath(filename)
  new_updatetime = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S'))
  new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S'))
  os.utime(filename, (new_access_time, new_updatetime))


if __name__ == '__main__':
  set_file_time('E:/a.txt', '2018-01-08 10:50:20', '2019-07-15 04:03:01')

 3、放在同一個(gè)py方便直接復(fù)制使用

# -*- encoding=utf-8 -*-
import os
import time


def get_file_time(filename):
  filename = os.path.abspath(filename)
  # 創(chuàng)建時(shí)間
  create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(filename)))
  # 修改時(shí)間
  update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename)))
  # 訪問(wèn)時(shí)間
  access_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getatime(filename)))
  return create_time, update_time, access_time


def set_file_time(filename, updatetime, access_time):
  # 先傳修改時(shí)間,再傳訪問(wèn)時(shí)間
  filename = os.path.abspath(filename)
  new_update_time = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S'))
  new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S'))
  os.utime(filename, (new_access_time, new_update_time))


def debug():
  create_time, update_time, access_time = get_file_time('E:/a.txt')
  set_file_time('E:/a.txt', update_time, access_time)
  get_file_time('E:/a.txt')


if __name__ == '__main__':

  debug()

 4、補(bǔ)充修改文件的創(chuàng)建時(shí)間

import os
import time

from pywintypes import Time # 可以忽視這個(gè) Time 報(bào)錯(cuò)(運(yùn)行程序還是沒(méi)問(wèn)題的)
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


def modify_file_create_time(filename, create_time_str, update_time_str, access_time_str):
  try:
    format_str = "%Y-%m-%d %H:%M:%S" # 時(shí)間格式
    # f = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
    f = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS, 0)
    create_time = Time(time.mktime(time.strptime(create_time_str, format_str)))
    update_time = Time(time.mktime(time.strptime(update_time_str, format_str)))
    access_time = Time(time.mktime(time.strptime(access_time_str, format_str)))
    SetFileTime(f, create_time, update_time, access_time)
    CloseHandle(f)
    print('update file time success:{}/{}/{}'.format(create_time_str, update_time_str,
                             access_time_str))
  except Exception as e:
    print('update file time fail:{}'.format(e))


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" # 訪問(wèn)時(shí)間
  fName = r"a.txt" # 可以是文件也可以是文件夾
  print(os.path.isdir(fName))
  modify_file_create_time(fName, cTime, mTime, aTime)

以上就是python如何修改文件時(shí)間屬性的詳細(xì)內(nèi)容,更多關(guān)于python修改文件時(shí)間屬性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python 實(shí)現(xiàn)樸素貝葉斯算法的示例

    python 實(shí)現(xiàn)樸素貝葉斯算法的示例

    這篇文章主要介紹了python實(shí)現(xiàn)樸素貝葉斯算法的示例,幫助大家更好的理解和學(xué)習(xí)python 機(jī)器學(xué)習(xí)算法,感興趣的朋友可以了解下
    2020-09-09
  • 利用soaplib搭建webservice詳細(xì)步驟和實(shí)例代碼

    利用soaplib搭建webservice詳細(xì)步驟和實(shí)例代碼

    這篇文章主要介紹了使用python soaplib搭建webservice詳細(xì)步驟和實(shí)例代碼,大家可以參考使用
    2013-11-11
  • Python中函數(shù)調(diào)用9大方法小結(jié)

    Python中函數(shù)調(diào)用9大方法小結(jié)

    在Python中,函數(shù)是一種非常重要的編程概念,它們使得代碼模塊化、可重用,并且能夠提高代碼的可讀性,本文將深入探討Python函數(shù)調(diào)用的9種方法,需要的可以參考下
    2024-01-01
  • python實(shí)現(xiàn)數(shù)組插入新元素的方法

    python實(shí)現(xiàn)數(shù)組插入新元素的方法

    這篇文章主要介紹了python實(shí)現(xiàn)數(shù)組插入新元素的方法,涉及Python中insert方法的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python中pytest命令行實(shí)現(xiàn)環(huán)境切換

    Python中pytest命令行實(shí)現(xiàn)環(huán)境切換

    在自動(dòng)化測(cè)試過(guò)程中經(jīng)常需要在不同的環(huán)境下進(jìn)行測(cè)試驗(yàn)證,所以寫(xiě)自動(dòng)化測(cè)試代碼時(shí)需要考慮不同環(huán)境切換的情況,本文主要介紹了Python中pytest命令行實(shí)現(xiàn)環(huán)境切換,感興趣的可以了解一下
    2023-07-07
  • 利用python如何處理nc數(shù)據(jù)詳解

    利用python如何處理nc數(shù)據(jù)詳解

    目前很多數(shù)據(jù)以nc格式存儲(chǔ),下面這篇文章主要給大家介紹了關(guān)于利用python如何處理nc數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。需要的朋友們下面來(lái)一起看看吧
    2018-05-05
  • Python??MkDocs優(yōu)雅地編寫(xiě)文檔

    Python??MkDocs優(yōu)雅地編寫(xiě)文檔

    在軟件開(kāi)發(fā)過(guò)程中,編寫(xiě)文檔是非常重要的一環(huán),文檔不僅可以幫助用戶理解和使用你的軟件,還可以提高團(tuán)隊(duì)協(xié)作效率,然傳統(tǒng)的文檔寫(xiě)作方式往往繁瑣而復(fù)雜,不易于維護(hù)更新,MkDocs工具以簡(jiǎn)潔、優(yōu)雅的方式編寫(xiě)文檔,并且能夠輕松生成漂亮的靜態(tài)網(wǎng)站
    2024-01-01
  • python文件比較示例分享

    python文件比較示例分享

    本文介紹了Python比較兩個(gè)文本文件內(nèi)容,如果不同, 給出第一個(gè)不同處的行號(hào)和列號(hào),大家參考使用吧
    2014-01-01
  • django之使用celery-把耗時(shí)程序放到celery里面執(zhí)行的方法

    django之使用celery-把耗時(shí)程序放到celery里面執(zhí)行的方法

    今天小編就為大家分享一篇django之使用celery-把耗時(shí)程序放到celery里面執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • Anaconda環(huán)境變量的配置圖文詳解

    Anaconda環(huán)境變量的配置圖文詳解

    Anaconda指的是一個(gè)開(kāi)源的Python發(fā)行版本,其包含了conda、Python等180多個(gè)科學(xué)包及其依賴項(xiàng),下面這篇文章主要給大家介紹了關(guān)于Anaconda環(huán)境變量配置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08

最新評(píng)論