python如何修改文件时间属性


Posted in Python onFebruary 05, 2021

1、获取文件的创建、修改、访问时间

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


def get_file_time(filename):
  filename = os.path.abspath(filename)
  create_time = os.path.getctime(filename) # 创建时间
  print('old create time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))))
  update_time = os.path.getmtime(filename) # 修改时间
  print('old update time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_time))))
  access_time = os.path.getatime(filename) # 访问时间
  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')

python如何修改文件时间属性

python如何修改文件时间属性

 2、更改文件的修改、访问时间(创建时间没查到怎么修改,暂时不记录)

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

def set_file_time(filename, updatetime, access_time):
  # 先传修改时间,再传访问时间
  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')

python如何修改文件时间属性

 3、放在同一个py方便直接复制使用

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


def get_file_time(filename):
  filename = os.path.abspath(filename)
  # 创建时间
  create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(filename)))
  # 修改时间
  update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename)))
  # 访问时间
  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):
  # 先传修改时间,再传访问时间
  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、补充修改文件的创建时间

import os
import time

from pywintypes import Time # 可以忽视这个 Time 报错(运行程序还是没问题的)
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" # 时间格式
    # 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" # 创建时间
  mTime = "2019-02-02 00:01:03" # 修改时间
  aTime = "2019-02-02 00:01:04" # 访问时间
  fName = r"a.txt" # 可以是文件也可以是文件夹
  print(os.path.isdir(fName))
  modify_file_create_time(fName, cTime, mTime, aTime)

以上就是python如何修改文件时间属性的详细内容,更多关于python修改文件时间属性的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python实现bucket排序算法实例分析
May 04 Python
Python编写简单的HTML页面合并脚本
Jul 11 Python
python读取二进制mnist实例详解
May 31 Python
Python爬取十篇新闻统计TF-IDF
Jan 03 Python
Java实现的执行python脚本工具类示例【使用jython.jar】
Mar 29 Python
详解pyqt5 动画在QThread线程中无法运行问题
May 05 Python
python并发编程多进程 模拟抢票实现过程
Aug 20 Python
在Python中用GDAL实现矢量对栅格的切割实例
Mar 11 Python
django 多数据库及分库实现方式
Apr 01 Python
Python logging模块原理解析及应用
Aug 13 Python
python3实现名片管理系统(控制台版)
Nov 29 Python
教你使用Python pypinyin库实现汉字转拼音
May 27 Python
虚拟环境及venv和virtualenv的区别说明
Feb 05 #Python
Pycharm 如何一键加引号的方法步骤
Feb 05 #Python
Python tkinter之Bind(绑定事件)的使用示例
Feb 05 #Python
pycharm配置python 设置pip安装源为豆瓣源
Feb 05 #Python
在PyCharm中安装PaddlePaddle的方法
Feb 05 #Python
python实现录制全屏和选择区域录屏功能
Feb 05 #Python
pycharm 使用anaconda为默认环境的操作
Feb 05 #Python
You might like
用PHP将数据导入到Foxmail
2006/10/09 PHP
在PHP中使用反射技术的架构插件使用说明
2010/05/18 PHP
深入理解PHP中的global
2014/08/19 PHP
初识PHP
2014/09/28 PHP
php几个预定义变量$_SERVER用法小结
2014/11/07 PHP
PHP会话控制实例分析
2016/12/24 PHP
PHP框架自动加载类文件原理详解
2017/06/06 PHP
Maps Javascript
2007/01/22 Javascript
根据地区不同显示时间的javascript代码
2007/08/13 Javascript
jQuery源码分析之Event事件分析
2010/06/07 Javascript
js自定义回调函数
2015/12/13 Javascript
Vue.js第一天学习笔记(数据的双向绑定、常用指令)
2016/12/01 Javascript
xmlplus组件设计系列之分隔框(DividedBox)(8)
2017/05/02 Javascript
nodejs操作mongodb的增删改查功能实例
2017/11/09 NodeJs
Layer组件多个iframe弹出层打开与关闭及参数传递的方法
2019/09/25 Javascript
Python中shutil模块的常用文件操作函数用法示例
2016/07/05 Python
python学习必备知识汇总
2017/09/08 Python
Python数据结构之图的应用示例
2018/05/11 Python
python中从str中提取元素到list以及将list转换为str的方法
2018/06/26 Python
python寻找list中最大值、最小值并返回其所在位置的方法
2018/06/27 Python
Python操作word常见方法示例【win32com与docx模块】
2018/07/17 Python
Python中修改字符串的四种方法
2018/11/02 Python
Python异常处理例题整理
2019/07/07 Python
通过实例解析python and和or使用方法
2020/11/14 Python
python 基于opencv 绘制图像轮廓
2020/12/11 Python
Bobbi Brown芭比波朗美国官网:化妆师专业彩妆保养品品牌
2016/08/18 全球购物
Nordgreen台湾官网:极简北欧设计手表
2019/08/21 全球购物
什么是Smart Navigation?
2016/07/03 面试题
销售行政专员职责
2014/01/03 职场文书
工作交流会欢迎词
2014/01/12 职场文书
网吧最新创业计划书范文
2014/03/27 职场文书
帮一个朋友写的求职信
2014/08/09 职场文书
教师群众路线剖析材料
2014/09/29 职场文书
公司员工奖惩制度
2015/08/04 职场文书
教你解决往mysql数据库中存入汉字报错的方法
2021/05/06 MySQL
golang中的struct操作
2021/11/11 Golang