<code class="language-python"># <a href="https://www.gaodaima.com/tag/print" title="查看更多关于print的文章" target="_blank">print</a>(<a href="https://www.gaodaima.com/tag/sys" title="查看更多关于sys的文章" target="_blank">sys</a>.version) #python 版本 # print(sys.path) # print(sys.platform) #当前什么系统 # print(sys.argv) #当前目录 </code>
www#gaodaima.com来源gaodai#ma#com搞@代~码网搞代码
一、hashlib、OS、Random、sys、zipfile模块学习、练习
1、hashlib模块
hashlib模块主要作用是用于信息的加密,其中他包括了许多算法,今天就说MD5,MD5
底层算法是哈希算法实现的,具体是什么我也不知道,总之是一个很nb的算法来加的密。
话不多说,直接上例子
<code class="language-python">import hashlib m=hashlib.md5() m.update("Hello空空荡荡".encode(encoding="utf-8")) print(m.hexdigest()) </code>
运行结果:
2、OS模块
os模块主要是用于和系统交互的,
<code class="language-python"># os.mkdir() #不可递归创建目录 #os.rmdir() 删除单个为空的目录 #os.makedirs(r"c:ac") #递归创建目录 #os.removedirs(r"c:ac") #目录为空,则删除,并递归到上一级,如若也为空,则删除,以此类推 print(os.listdir()) #列出当前目录下的文件 # os.remove("") #删除一个文件 # os.rename("") #重命名 # print(os.sep) #输出操作系统特定的路径分隔符 win是,Linux/ # print(os.pathsep) #输出分割文件路径的字符串 # print(os.linesep) #输出当前平台的行终止符 win linux # print(os.environ) #获取系统环境变量 # print(os.name) #获取使用平台 win:nt linex:posix # os.system("dir") #运行命令 #print(os.path.exists(r"c:Python32")) #输入的路径是否存在 </code>
3、Random
random模块主要作用是各种分布的随机数生成器
<code class="language-python">import random print(random.randint(1,10)) #1-10都包含 print(random.randrange(1,10)) #包含前面数字,不包括后面 print(random.randrange(0,101,2)) #0-100之间的偶数 print(random.choice("hello")) #从序列中获取一个随机字符 print(random.sample("kongming",2)) #从序列中获取2个随机数 #随机浮动数 print(random.random()) print(random.uniform(0,10)) #洗牌 items=[1,2,3,4,5,6] random.shuffle(items) #把原来的顺序打乱 print(items) </code>
用random模块做的一个随机验证码 :
<code class="language-python">#Python学习交流群:778463939 import random captcha="" for i in range(6): chank=random.randrange(0,6) if chank == i: tem=chr(random.randint(65,90)) else: tem=random.randint(0,9) captcha+=str(tem) print(captcha) </code>
4、sys模块
该模块提供对解释器使用或维护的一些变量的访问,以及与解释器强烈交互的函数
<code class="language-python"> # print(sys.version) #python 版本 # print(sys.path) # print(sys.platform) #当前什么系统 # print(sys.argv) #当前目录 </code>
5、zipfile模块
使用 zipfile 压缩文件
<code class="language-python">import zipfile z = zipfile.ZipFile("day5.zip","w") z.write("2.txt") print("-----") z.write("1.txt") z.close() </code>