首页 > 编程语言 >python-时间操作

python-时间操作

时间:2022-10-14 11:44:36浏览次数:48  
标签:filePath python get datetime 时间 time 操作 path now

1. 获取当前时间

import time

# 获取当前日期时间  返回值:当前日期时间
def get_now_time():
  now = time.localtime()
  now_time = time.strftime("%Y-%m-%d %H:%M:%S", now)
  return now_time


if __name__ == '__main__':
  now_time = get_now_time()
  print(now_time)

 

2. 日期减掉特定时长

import datetime
today=datetime.datetime.now()
datetime1=today-datetime.timedelta(seconds=10) #减10秒
datetime2=today-datetime.timedelta(minutes=10) #减10分钟
datetime3=today-datetime.timedelta(hours=1) #减1小时
datetime4=today-datetime.timedelta(days=1)  #减1天
datetime5=today-datetime.timedelta(weeks=1) #减1周
datetime6=today.strftime('%Y-%m-%d') #将时间格式化为字符串

 

3. 计算2个时间戳之差

  import datetime

  time1 = datetime.datetime.fromtimestamp(start_time)

  time2 = datetime.datetime.fromtimestamp(end_time)

  time_diff = time2-time1

  print(time_diff)

  print(time_diff.total_seconds())

 

4. 获取文件的修改时间、访问时间、创建时间、大小占用

# -*- coding: UTF8 -*-
import os
import time


def TimeStampToTime(timestamp):
  timeStruct = time.localtime(timestamp)
  return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)


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


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


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


def get_FileSize(filePath):
  # '''获取文件的大小,结果保留两位小数,单位为MB'''

  fsize = os.path.getsize(filePath)
  fsize = fsize / float(1024 * 1024)
  return round(fsize, 2)


if __name__ == '__main__':
  path = r"/opt/test"
  print(get_FileCreateTime(path))
  print(get_FileModifyTime(path))
  print(get_FileAccessTime(path))
  print(get_FileSize(path))

 

 

 

 

 

 

标签:filePath,python,get,datetime,时间,time,操作,path,now
From: https://www.cnblogs.com/shg104/p/16787251.html

相关文章

  • Python人工智能经典算法之线性回归
    1.9k近邻算法总结[**]优点:1.简单有效2.重新训练代价底3.适合类域交叉样本4.适合大样本自动分类缺点:1.惰性学习2......
  • Python爬虫之数据提取概述
    数据提取概述知识点了解响应内容的分类了解xml和html的区别1.响应内容的分类在发送请求获取响应之后,可能存在多种不同类型的响应内容;而且很多时候,我们只需要响应内容中的......
  • Python爬虫之数据提取-selenium定位获取标签对象并提取数据
    selenium提取数据知识点:了解driver对象的常用属性和方法掌握driver对象定位标签元素获取标签对象的方法掌握标签对象提取文本和属性值的方法1.driver对象的常用属性和方......
  • Python爬虫之scrapy的日志信息与配置
    scrapy的日志信息与配置学习目标:了解scrapy的日志信息掌握scrapy的常用配置掌握scrapy_redis配置了解scrapy_splash配置了解scrapy_redis和scrapy_splash配合使用的配置1......
  • Python爬虫之数据提取-selenium的其它使用方法
    selenium的其它使用方法知识点:掌握selenium控制标签页的切换掌握selenium控制iframe的切换掌握利用selenium获取cookie的方法掌握手动实现页面等待掌握selenium控制浏......
  • Python爬虫之mongodb的聚合操作
    mongodb的聚合操作学习目标了解mongodb的聚合原理掌握mongdb的管道命令掌握mongdb的表达式1mongodb的聚合是什么聚合(aggregate)是基于数据处理的聚合管道,每个文档通过......
  • Python爬虫之mongodb的索引操作
    Mongodb的索引操作学习目标掌握mongodb索引的创建,删除操作掌握mongodb查看索引的方法掌握mongodb创建唯一索引的方法1.为什么mongdb需要创建索引加快查询速度进行数据的......
  • Python爬虫之scrapy构造并发送请求
    scrapy数据建模与请求学习目标:应用在scrapy项目中进行建模应用构造Request对象,并发送请求应用利用meta参数在不同的解析函数中传递数据1.数据建模通常在做项目的过程中,......
  • Python爬虫之scrapy模拟登陆
    scrapy模拟登陆学习目标:应用请求对象cookies参数的使用了解start_requests函数的作用应用构造并发送post请求1.回顾之前的模拟登陆的方法1.1requests模块是如何实现模......
  • Python爬虫之scrapy_redis原理分析并实现断点续爬以及分布式爬虫
    scrapy_redis原理分析并实现断点续爬以及分布式爬虫学习目标了解scrapy实现去重的原理了解scrapy中请求入队的条件掌握scrapy_redis基于url地址的增量式单机爬虫掌握scr......