ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python基础入门(8)- Python模块和包

2021-12-30 10:02:01  阅读:180  来源: 互联网

标签:入门 Python datetime tm 模块 time print import now


1.包与模块的定义与导入

1.1.什么是python的包与模块

  • 包就是文件夹,包中还可以有包,也就是子文件夹
  • 一个个python文件模块


1.2.包的身份证

__init__.py是每一个python包里面必须存在的文件,这个文件里面可以没有任何内容


1.3.如何创建包

  • 要有一个主题,明确功能,方便使用
  • 层次分明,调用清晰
  • 文件里面要有包的身份认证文件,即__init__.py文件,证明该文件夹是一个包


1.4.包的导入

虽然说图示animal包里面还有子包子文件,但是import animal只能拿到当前包__init__.py里面的功能;当import具体文件时,比如import test1.py只能拿当前模块(当前文件)中的功能,同级animal包中的功能无法访问使用


1.5.模块的导入

 

2.第三方包

  • 阿里云 http://mirrors.aliyun.com/pypi/simple/
  • 豆瓣http://pypi.douban.com/simple/
  • 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  • 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
  • 华中科技大学http://pypi.hustunique.com/
  • 使用格式:如安装ipython版本为19.0.1的包pip install -i http://mirrors.aliyun.com/pypi/simple/ ipython==19.0.1

 

3.Python的datetime与time

  • datetime和time这两个包为python中常用的两个时间包
  • datetime常用于对日期的处理
  • time常用于对时间、计时等处理

3.1.datetime

  • 日期与时间的结合体-date and time
  • 获取当前时间
  • 获取时间间隔
  • 将时间对象转成时间字符串
  • 将字符串转成时间类型

3.1.1.datetime包的常用功能

获取当前时间

获取时间间隔

 1 # coding:utf-8
 2 
 3 from datetime import datetime
 4 from datetime import timedelta
 5 
 6 now=datetime.now()
 7 print(now,type(now))    #2021-12-27 16:19:18.586413 <class 'datetime.datetime'>
 8 
 9 three_days=timedelta(days=3)
10 print(type(three_days)) #<class 'datetime.timedelta'>
11 
12 after_three_day=now+three_days
13 print(after_three_day)  #2021-12-30 16:19:18.586413

时间对象转字符串

1 # coding:utf-8
2 
3 from datetime import datetime
4 
5 now=datetime.now()
6 now_str=now.strftime('%Y-%m-%d %H:%M:%S')
7 print(now_str,type(now_str))    #2021-12-27 16:28:06 <class 'str'> 日期字符串无法实现日期的加减法,必须转成日期对象类型才能实现日期的加减法

时间字符串转时间类型

 1 # coding:utf-8
 2 
 3 from datetime import datetime
 4 from datetime import timedelta
 5 
 6 now=datetime.now()
 7 now_str=now.strftime('%Y-%m-%d %H:%M:%S')
 8 print(now_str,type(now_str))    #2021-12-27 16:37:11 <class 'str'>
 9 
10 now_obj=datetime.strptime(now_str,'%Y-%m-%d %H:%M:%S')
11 print(now_obj,type(now_obj))    #2021-12-27 16:37:11 <class 'datetime.datetime'>,转成对象的时候,后面的格式必须得跟字符串的格式匹配
12 
13 three_days=timedelta(days=3)
14 after_three_day=now_obj+three_days
15 print(after_three_day,type(after_three_day))    #2021-12-30 16:37:11 <class 'datetime.datetime'>

3.1.2.python的常用时间格式化符号

1 # coding:utf-8
2 
3 from datetime import datetime
4 
5 now=datetime.now()
6 now_str=now.strftime('%Y-%m-%d %H:%M:%S %p %j %U %A')
7 print(now_str,type(now_str))    #2021-12-27 16:45:31 PM 361 52 Monday <class 'str'>

3.2.time

3.2.1认识时间戳

  • 1970年1月1日00时00分00秒至今的总毫秒(秒)数
  • 使用timestamp代表时间戳
  • 时间戳是float类型的

3.2.2认识python的time模块与常用方法

生成时间戳函数time

1 # coding:utf-8
2 import time
3 
4 now=time.time()
5 print(now,type(now))    #1640595949.671707 <class 'float'>

获取本地时间函数localtime

timestamp不传代表当前时间

1 # coding:utf-8
2 import time
3 
4 now=time.time()
5 time_obj=time.localtime(now)
6 print(time_obj,type(time_obj))    #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=6, tm_sec=43, tm_wday=0, tm_yday=361, tm_isdst=0)  <class 'time.struct_time'>

localtime对应字段介绍

1 # coding:utf-8
2 import time
3 
4 now=time.localtime()
5 print(now)  #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=1, tm_sec=12, tm_wday=0, tm_yday=361, tm_isdst=0)

暂停函数sleep

1 # coding:utf-8
2 import time
3 
4 for i in range(10):
5     print(i)
6     time.sleep(1)

time中的strftime与strptime

 1 # coding:utf-8
 2 import time
 3 
 4 now=time.time()
 5 print(now,type(now))    #1640596914.3263566 <class 'float'>
 6 
 7 #now_str=time.strftime('%Y-%m-%d %H:%M:%S',now)  #TypeError: Tuple or struct_time argument required 报错,因为now时间戳是浮点型,不是time.localtime对应的时间类型
 8 print(type(time.localtime()))   #<class 'time.struct_time'>
 9 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
10 print(now_str,type(now_str))    #2021-12-27 17:21:54 <class 'str'>

1 # coding:utf-8
2 import time
3 
4 now=time.time()
5 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
6 now_obj=time.strptime(now_str,'%Y-%m-%d %H:%M:%S')
7 print(now_obj,type(now_obj))    #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=18, tm_min=5, tm_sec=40, tm_wday=0, tm_yday=361, tm_isdst=-1) <class 'time.struct_time'>

3.2.3datetime转时间戳与datetime时间戳转时间对象的方法

datetime转时间戳

1 # coding:utf-8
2 
3 from datetime import datetime
4 
5 now=datetime.now()
6 now_stamp=datetime.timestamp(now)
7 print(now_stamp,type(now_stamp))    #1640600288.843319 <class 'float'>

datetime时间戳以及time时间戳转时间对象

 1 # coding:utf-8
 2 
 3 from datetime import datetime
 4 import time
 5 
 6 now=datetime.now()
 7 now_stamp=datetime.timestamp(now)
 8 now_stamp_obj=datetime.fromtimestamp(now_stamp)
 9 print(now_stamp_obj,type(now_stamp_obj))    #2021-12-27 18:56:15.849622 <class 'datetime.datetime'>
10 
11 now_time=time.time()
12 now_time_obj=datetime.fromtimestamp(now_time)
13 print(now_time_obj,type(now_time_obj))  #2021-12-27 18:56:15.849623 <class 'datetime.datetime'>

 

4.Python内置库os与sys模块

4.1.os模块

4.1.1.os的文件与目录函数介绍

 1 # coding:utf-8
 2 
 3 import os
 4 
 5 current_path=os.getcwd()
 6 print(current_path)
 7 
 8 new_path='%s/test1/test2' % current_path
 9 os.makedirs(new_path)
10 
11 os.removedirs('test1/test2')
12 os.rename('test1','test')
13 data=os.listdir(current_path)
14 print(data)

4.1.2.os.path模块常用函数介绍


4.2.sys模块

 

 

标签:入门,Python,datetime,tm,模块,time,print,import,now
来源: https://www.cnblogs.com/gltou/p/15735424.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有