随笔 - 654  文章 - 1  评论 - 76  阅读 - 319万

python 获取文件大小,创建时间和访问时间

# -*- coding: UTF8 -*-

import time
import datetime

import os

 

1、  '''把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12'''
    def TimeStampToTime(timestamp):
      timeStruct = time.localtime(timestamp)
      return time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)

 

2、  '''获取文件的大小,结果保留两位小数,单位为MB'''
    def get_FileSize(filePath):
      filePath = unicode(filePath,'utf8')
      fsize = os.path.getsize(filePath)
      fsize = fsize/float(1024*1024)
      return round(fsize,2)


3、  '''获取文件的访问时间'''
    def get_FileAccessTime(filePath):
      filePath = unicode(filePath,'utf8')
      t = os.path.getatime(filePath)
      return TimeStampToTime(t)


4、  '''获取文件的创建时间'''
    def get_FileCreateTime(filePath):
      filePath = unicode(filePath,'utf8')
      t = os.path.getctime(filePath)
      return TimeStampToTime(t)


5、  '''获取文件的修改时间'''
    def get_FileModifyTime(filePath):
      filePath = unicode(filePath,'utf8')
      t = os.path.getmtime(filePath)
      return TimeStampToTime(t)

posted on   shaomine  阅读(133751)  评论(2编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示