day5主要是各种常用模块的学习
- time &datetime模块
- random
- os
- sys
- shutil
- json & picle
- shelve
- xml处理
- yaml处理
- configparser
- hashlib
- subprocess
- logging模块
- re正则表达式
time & datetime模块
#Authon Ivor
import time
#打印时间戳
print(time.time())
#打印当前时间的时间对象格式
print(time.localtime())
#字符串转换时间对象
print(time.strptime("2016-02-02 15:52:20","%Y-%m-%d %H:%M:%S"))
#时间对象转换字符串
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
#时间对象转换时间戳
print(time.mktime(time.localtime()))
#时间戳转换时间对象
print(time.gmtime(time.time()))
import datetime
#打印当前日期时间
print(datetime.datetime.now())
#从时间戳转换成年月日格式
print(datetime.date.fromtimestamp(time.time()))
#从时间戳转换成年月日时间格式
print(datetime.datetime.fromtimestamp(time.time()))
#对当前时间进行加减运算
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(hours=-3))
#直接进行时间替换
t = datetime.datetime.now()
print(t.replace(year=2012,month=12,day=24,hour=0,minute=0,second=0))
random模块
#Authon Ivor
import random
for i in range(10):
print(random.random())
print(random.randint(1,5))
print(random.randrange(1,5))
shutil 模块
#Authon Ivor
import shutil
#拷贝文件对象
f1 = open("random mod.py")
f2 = open("random_new.py","w")
shutil.copyfileobj(f1,f2,length=1)
#拷贝文件,文件和权限,包括copyfile和copymode
shutil.copy("random mod.py","random_new.py")
#拷贝文件和状态信息,包括copyfile和copystat
shutil.copy2("random mod.py","random_new.py")
#拷贝文件,不包括权限
shutil.copyfile("random mod.py","random_new.py")
#仅拷贝文件权限,仅拷贝权限。内容、组、用户均不变
shutil.copymode("random mod.py","random_new.py")
#拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat("random mod.py","random_new.py")
#拷贝文件目录,递归拷贝
shutil.copytree()
#递归删除
shutil.rmtree()
#移动文件
shutil.move()
shutil.make_archive()
#base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
# 如:www =>保存至当前路径
# 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
# •format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
# •root_dir: 要压缩的文件夹路径(默认当前目录)
# •owner: 用户,默认当前用户
# •group: 组,默认当前组
# •logger: 用于记录日志,通常是logging.Logger对象
import zipfile
# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()
# zipfile 压缩解压
import tarfile
# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close()
# 解压
tar = tarfile.open('your.tar','r')
tar.extractall() # 可设置解压地址
tar.close()
json & pickle 模块
#Authon Ivor import json # info = { # "name":"Ivor", # "age":"27", #} info = ["a","b","c","f","f","g"] with open("jsondump.json","w") as f: f.write(json.dumps(info)) with open("jsondump.json","w") as f: json.dump(info,f) #Authon Ivor import json info = { "name":"Ivor", "age":"27", } with open("jsondump.json","r") as f: a = json.loads(f.read()) with open("jsondump.json","r") as f: b = json.load(f) print(a,b) #Authon Ivor import pickle def sayhi(): print("Hello World!") info = { "name":"Ivor", "func":sayhi } with open("pickle.pickle","wb") as f: f.write(pickle.dumps(info)) with open("pickle.pickle", "wb") as f: pickle.dump(info,f) #Authon Ivor import pickle def sayhi(): print("Hello World!") with open("pickle.pickle","rb") as f: pickle.loads(f.read()) with open("pickle.pickle","rb") as f: pickle.load(f)