python学习笔记(六)— 模块
一、os、sys模块
import os
print(os.getcwd())#取当前工作目录,绝对路径
print(os.chdir("../"))#更改当前目录 print(os.pardir) # 父目录,相对路径
print(os.curdir)#当前目录,相对路径 os.rmdir("a")#删除指定的文件夹,空文件夹
os.remove("test") # 删除文件
os.rename("test1","test")#重命名 print(os.listdir('.'))#列出一个目录下的所有文件
print(os.stat("f2"))#获取文件信息 print(__file__)#__file__是这个文件的绝对路径
print(os.path.abspath(__file__))#获取绝对路径 print(os.path.split("/usr/hehe/hehe.txt")) # 分割路径和文件名
print(os.path.dirname("/day5/f1")) # 获取父目录
print(os.path.basename("/day5/f1"))#获取最后一级,如果是文件显示文件名,如果是目录显示目录名 print(os.path.exists("c://test")) # 目录/文件是否存在
print(os.path.isfile("test"))#判断是否是一个文件
print(os.path.isdir("D:\资料\笔记\Python\day5"))#是否是一个文件夹
print(os.path.join("root",'hehe','a.sql'))#拼接成一个路径 print(os.stat("f1")) # 获取文件信息
print(os.sep) # 当前操作系统的路径分隔符
print(os.linesep) # 当前操作系统的换行符
print(os.pathsep) # 当前系统的环境变量中每个路径的分隔符,linux是:,windows是;
print(os.environ) # 当前系统的环境变量
print(os.name) # 当前系统名称 os.system('ipconfig')#执行操作系统命令,只能执行,不能获取结果
res=os.popen('ipconfig')#执行操作系统命令,并且可以获取返回结果
print(res.read())
import sys
print(sys.argv)#命令行参数List,第一个元素是程序本身路径
sys.exit(n)#退出程序,正常退出时exit(0)
sys.version#获取Python解释程序的版本信息
sys.maxint#最大的Int值
sys.path#返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform#返回操作系统平台名称
sys.stdout.write('please:') # 向屏幕输出一句话
val = sys.stdin.readline()[:-1] # 获取输入的值
二、time、datetime模块
import datetime,time
time.sleep(1)#休息几s
print(time.timezone)#和标准时间相差的时间,单位是s
print(time.time())#获取当前时间戳
#时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总秒数
'''时间戳和格式化好的时间相互转换的话,要转换成时间元组'''
print(time.gmtime(181818881))#默认取标准时区的时间戳,输入一个时间戳把时间戳转换成时间元组
print(time.localtime(181818881))#默认取当前时区的时间戳,输入一个时间戳把时间戳转换成时间元组(北京当前时区,比标准多8小时)
print(time.mktime(time.localtime()))#把时间元组转换成时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S"))#将时间元组转换成格式化输出的字符串
print(time.strptime("20160204 191919","%Y%m%d %H%M%S"))#将格式化的时间转换成时间元组
print(time.struct_time)#时间元组
print(time.asctime())#时间元转换成格式化时间
print(time.ctime())#时间戳转换成格式化时间
print(datetime.datetime.now())#当然时间格式化输出
print(datetime.datetime.now()+datetime.timedelta(3))#3天后的时间
print(datetime.datetime.now()+datetime.timedelta(-3))#3天前的时间
def timestampToStr(time_strmp,format='%Y%m%d%H%M%S'):#时间戳格式化好的时间
cur_time=time.localtime(time_strmp)
res=time.strftime(format,cur_time)
return res
def strToTimestamp(time_st,format='%Y%m%d%H%M%S'):#格式化好的时间转时间戳
t = time.strftime(time_st, format)
res =time.mktime(t)
return res
print(timestampToStr(202020))
print(strToTimestamp("20160204","%Y%m%d"))
三、加密模块
import hashlib
'''md5'''
a='HHh'
m = hashlib.md5()
bytes_a=a.encode()#加密不能传字符串,要输入二进制类型
m.update(bytes_a)#加密
print(m.hexdigest())#加密后的结果,Md5不可逆,不能被解密
def Md5_psw(st:str):#限定了入参的类型
m = hashlib.md5()
bytes_st=st.encode()
m.update(bytes_st)
return m.hexdigest()
res=Md5_psw('hhh')
print(res)
'''shal'''
hash = hashlib.sha1()
print(hash.hexdigest())
'''sha266'''
hash = hashlib.sha256()
print(hash.hexdigest())
'''sha384'''
hash = hashlib.sha384()
print(hash.hexdigest())
'''sha512'''
hash = hashlib.sha512()
print(hash.hexdigest())
'''base64'''
import base64
a='ahha'
r=a.encode()#字符串变成二进制
res=base64.b64encode(r)#base64编码
print(res.decode())#转成字符串
print(base64.b64encode(res.decode()))
四、json的处理
import json
#json串就是字符串。
d = {
'car':{'color':'red','price':100,'count':50},
'挨粪叉':{'color':'red','price':100,'count':50},
'挨粪叉1':{'color':'red','price':100,'count':50},
'挨粪叉2':{'color':'red','price':100,'count':50},
'挨粪叉3':{'color':'red','price':100,'count':50},
'挨粪叉4':{'color':'red','price':100,'count':50},
} res = json.dumps(d,indent=8,ensure_ascii=False) #把list、字典转成json,indent多少缩进,ensure_ascii可以显示中文
f1 = open('f1','w',encoding='utf-8')
f1.write(res) f1 = open('f1',encoding='utf-8')
res = f1.read()
dict_res = json.loads(res) #把json串变成python的数据类型
print(dict_res) f1 = open('f1','w',encoding='utf-8')
json.dump(d,f1,ensure_ascii=False,indent=4)
#自动帮你写入文件,第一个参数是数据,第二个是文件对象 f1 = open('f1',encoding='utf-8')
print(json.load(f1))
#自动帮你读文件。
python学习笔记(六)— 模块的更多相关文章
- Python学习笔记六
Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...
- Python学习笔记之模块与包
一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...
- Python学习笔记—itertools模块
这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...
- python学习笔记_week5_模块
模块 一.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能), 本质就是.py结尾的python文件(文件名:test.py,对应模块名:test) 包:用来从逻辑上 ...
- python学习笔记(八)-模块
大型python程序以模块和包的形式组织.python标准库中包含大量的模块.一个python文件就是一个模块.1.标准模块 python自带的,不需要你安装的2.第三方模块 需要安装,别人提供的. ...
- Python学习笔记-常用模块
1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...
- Python学习笔记1—模块
模块的使用 引用模块的两种形式 形式一: import module_name 形式二: from module1 import module11 (module11是module的子模块) 例: ...
- Python学习笔记2——模块的发布
1.为模块nester创建文件夹nester,其中包含:nester.py(模块文件): """这是"nester.py"模块,提供了一个名为prin ...
- python学习笔记十——模块与函数
第五章 模块与函数 5.1 python程序的结构 函数+类->模块 模块+模块->包 函数+类+模块+包=Python pyth ...
- Python学习笔记14—模块
在python中所有的模块都被加入到了sys.path中,用下面的方法可以看见模块的位置. >>> import sys >>> import pprint > ...
随机推荐
- Spring Cloud心跳监测
Spring Cloud实现心跳监测,在服务注册和停止时,注册中心能得到通知,并更新服务实例列表 Spring Cloud注册中心添加配置: eureka.server.enable-self-pre ...
- js in
定义: in操作符用来判断某个属性属于某个对象,可以是对象的直接属性,也可以是通过prototype继承的属性.(参见hasOwnProperty) 注意事项: n 对于一般的对象属性 ...
- jQuery:(一)jQuery简介
一.jQuery简介jQuery由美国人John Resig于2006年创建jQuery是目前最流行的JavaScript程序库,它是对JavaScript对象和函数的封装. 二.jQuery的优势1 ...
- python的卸载方式和运行yum报错:No module named yum
公司测试机环境不知道给我卸了什么包,导致yum运行报错状况: 系统版本:Red Hat Enterprise Linux Server release 6.2 (Santiago) 内核版本:2.6. ...
- CI框架中 类名不能以方法名相同
昨天晚上一个坑爹的问题折腾了我一晚上,首先我来说下我的代码,我建立了一个index的控制器然后呢 在控制器里有一个index的方法.页面模板都有. if ( ! defined('BASEPATH' ...
- CI 如何获取get请求过来的数据
http://localhost/ci_tuangou/index.php/home/index/index?gid=2 echo 'gid='. $this->input->get('g ...
- 007杰信-factory的启用+停用
业务需求:当有一些factory与我们不在合作时,我们不能直接删除这个公司的数据,我们采用的办法是在factory_c表增加一个字段STATE(CHAR(1)),1表示是启用,0是表示停用. 准备工作 ...
- 漫游Kafka设计篇之性能优化(7)
Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...
- 使用 Estimator 构建卷积神经网络
来源于:https://tensorflow.google.cn/tutorials/estimators/cnn 强烈建议前往学习 tf.layers 模块提供一个可用于轻松构建神经网络的高级 AP ...
- ios开发之 -- Swap file ".Podfile.swp" already exists!