编程语言
首页 > 编程语言> > Python学习笔记:day16 模块基础知识(time\datetime\json\picle\shutil\logging)

Python学习笔记:day16 模块基础知识(time\datetime\json\picle\shutil\logging)

作者:互联网

今日内容

内容回顾和补充

模块(类库)

面试题:

定义模块

定义模块时可以把一个py文件或一个文件夹(包)当作一个模块,以方便于以后其他py文件的调用

对于包的定义:

模块的调用

示例一

def show():
    print('卫斯理作者')

def func():
    pass

print(456)
#导入模块
import lizhongwei

print(123)
#调用模块中的函数
lizhongwei.func()
导入模块
from lizhongwei import func,show
from lizhongwei import *
from lizhongwei import func

func()
#导入模块
from lizhongwei import func as f

def func():
    print(123)
    
f()

导入模块:

示例二:

lizhong
	- jd.py
    - pdd.py
    - tb.py
包.py
import lizhong.jd
lizhong.jd.f1()
from lizhong import jd
jd.f1()
from lizhong.jd import f1
f1()

总结

注意:sys.path的作用?

示例

import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

内置模块

今日内容

json和pickle

shutil模块

import shutil

#删除目录
# shutil.rmtree('test')

#重命名
# shutil.move('x.txt','xxx.txt')

#压缩文件
# shutil.make_archive('zzh','zip','D:\code\s21day16')

#解压文件
shutil.unpack_archive('zzh.zip',extract_dir=r'D:\code\xxx,format='zip')
示例:
import os
import shutil
from datetime import datetime

#1.压缩lizhongwei文件夹zip
#2.放到code目录(默认不存在)
#3.将文件解压到D:\x1目录中。
ctime = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
print(ctime)
if not os.path.exists('code'):
    os.makedirs('code')

shutil.make_archive(os.path.join('code',ctime),'zip','D:\code\s21day16\lizhongwei')
file_path = os.path.join('code',ctime) + '.zip'
shutil.unpack_archive(file_path,r'D:\x1','zip')

time & datetime

time模块

datetime模块

import time
from datetime import datetime,timezone,timedelta

###获取datetime格式时间
'''
v1 = datetime.now()  #当前本地时间
print(v1)
v2 = datetime.utcnow() #当前UTC时间
print(v2)
tz = timezone(timedelta(hours=7)) #当前东7区时间
v3 = datetime.now(tz)
print(v3)
'''
###把datetime格式转换成字符串
# v1 = datetime.now()
# print(v1,type(v1))
# val = v1.strftime('%Y-%m-%d %H:%M:%S')
# val = v1.strftime('%y-%m-%d %H:%M:%S')
# print(val)

###字符串转成datetime
v1 = datetime.strptime('2011-11-11','%Y-%m-%d')
print(v1,type(v1))

###datetime时间的加减
v1 = datetime.strptime('2011-11-11','%Y-%m-%d')
print(v1,type(v1))
v2 = v1 + timedelta(days=140)
print(v2)

###时间戳和datetime关系
ctime = time.time()
print(ctime)

v1 = datetime.fromtimestamp(ctime)
print(v1)

logging

异常处理

'''
try:
    val = input('请输入数字:')
    num = int(val)
except Exception as e:
    print('操作异常')
'''

# import requests
# try:
#     ret = requests.get('http://www.baidu.com')
#     print(ret.text)
# except Exception as e:
#     print('请求异常')

def func(a):
    try:
        a.strip()
    except Exception as e:
        pass
    return False

# v = func('alex')
v = func([11,22,33])
# print(v)
if not v:
    print('函数执行失败')
else:
    print('结果是',v)

练习题

#1.写函数,函数接收一个列表,请将列表中的元素每个都+100

def func(arg):
    result = []
    for item in arg:
        if item.isdecimal():
            result.append(int(item) + 100)
    return result

#2.写函数去接收一个列表,列表中都是url,请访问每个地址并获取结果。
import requests
def func(url_list):
    result = []
    try:
    	for url in url_list:
    		response = requests.get(url)
    		result.append(response.text)
    except Exception as e:
        pass
    erturn result
    
def func2(url_list):
    result = []
    for url in url_list:
        try:
        	response = requests.get(url)
    		result.append(response.text)
    	except Exception as e:
        	pass
    erturn result
    
func(['http://www.baidu.com','http://www.google.com','http://www.bing.com'])

总结

函数高级 5*

模块分类和自定义 4*

导入模块

异常处理

try:
    pass
except Exception as e:
    pass

标签:logging,val,Python,datetime,v1,模块,print,import,shutil
来源: https://www.cnblogs.com/leiyu567567/p/14853342.html