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

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

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

1、獲取文件的創(chuàng)建、修改、訪問時(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) # 訪問時(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、更改文件的修改、訪問時(shí)間(創(chuàng)建時(shí)間沒查到怎么修改,暫時(shí)不記錄)

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

def set_file_time(filename, updatetime, access_time):
  # 先傳修改時(shí)間,再傳訪問時(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)))
  # 訪問時(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í)間,再傳訪問時(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)行程序還是沒問題的)
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" # 訪問時(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)文章

  • python3發(fā)送郵件需要經(jīng)過代理服務(wù)器的示例代碼

    python3發(fā)送郵件需要經(jīng)過代理服務(wù)器的示例代碼

    今天小編就為大家分享一篇python3發(fā)送郵件需要經(jīng)過代理服務(wù)器的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python統(tǒng)計(jì)列表中元素出現(xiàn)次數(shù)的三種方法

    python統(tǒng)計(jì)列表中元素出現(xiàn)次數(shù)的三種方法

    這篇文章主要介紹了python統(tǒng)計(jì)列表中元素出現(xiàn)次數(shù)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Python3中的re.findall()方法及re.compile()

    Python3中的re.findall()方法及re.compile()

    這篇文章主要介紹了Python3中的re.findall()方法及re.compile(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python3 XML 獲取雅虎天氣的實(shí)現(xiàn)方法

    Python3 XML 獲取雅虎天氣的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇Python3 XML 獲取雅虎天氣的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 使用Python模擬操作windows應(yīng)用窗口詳解

    使用Python模擬操作windows應(yīng)用窗口詳解

    在日常工作中,我們經(jīng)常遇到需要進(jìn)行大量重復(fù)性任務(wù)的情況,這篇文章將介紹如何使用 Python 模擬操作記事本,感興趣的小伙伴可以了解下
    2025-02-02
  • python實(shí)現(xiàn)將漢字轉(zhuǎn)換成漢語拼音的庫

    python實(shí)現(xiàn)將漢字轉(zhuǎn)換成漢語拼音的庫

    這篇文章主要介紹了python實(shí)現(xiàn)將漢字轉(zhuǎn)換成漢語拼音的庫,涉及Python調(diào)用word.data字段實(shí)現(xiàn)將漢字轉(zhuǎn)換成拼音的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • Python Web框架Flask信號(hào)機(jī)制(signals)介紹

    Python Web框架Flask信號(hào)機(jī)制(signals)介紹

    這篇文章主要介紹了Python Web框架Flask信號(hào)機(jī)制(signals)介紹,本文介紹Flask的信號(hào)機(jī)制,講述信號(hào)的用途,并給出創(chuàng)建信號(hào)、訂閱信號(hào)、發(fā)送信號(hào)的方法,需要的朋友可以參考下
    2015-01-01
  • Python中的With語句的使用及原理

    Python中的With語句的使用及原理

    這篇文章主要介紹了Python中的With語句的使用及原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • python主要用于哪些方向

    python主要用于哪些方向

    在本篇文章里小編給大家整理了一篇關(guān)于python用于的方向的相關(guān)文章,有需要的閱讀下吧。
    2020-07-07
  • 對(duì)Keras自帶Loss Function的深入研究

    對(duì)Keras自帶Loss Function的深入研究

    這篇文章主要介紹了對(duì)Keras自帶Loss Function的深入研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論

黄骅市| 公安县| 东乌| 军事| 庆安县| 昭苏县| 饶河县| 隆回县| 府谷县| 浮山县| 长兴县| 南安市| 浑源县| 淮南市| 贵定县| 汶川县| 尼木县| 土默特右旗| 石狮市| 汝南县| 吉安市| 南平市| 永修县| 正镶白旗| 临夏市| 崇礼县| 伊金霍洛旗| 枣阳市| 金寨县| 平顶山市| 浦城县| 富阳市| 开原市| 平凉市| 北京市| 泽库县| 大渡口区| 九寨沟县| 菏泽市| 阿拉善左旗| 乌鲁木齐县|