1.调用shell命令
获取命令的执行结果
import os
result = os.popen("ping baidu.com")
print(result)
print(type(result))
print(result.read())
#shell执行的结果都保存在result中 把他当作一个对象读出来 在输出里面的内容
<os._wrap_close object at 0x0000020471025190>
<class 'os._wrap_close'>
正在 Ping baidu.com [39.156.66.10] 具有 32 字节的数据:
来自 39.156.66.10 的回复: 字节=32 时间=24ms TTL=49
来自 39.156.66.10 的回复: 字节=32 时间=30ms TTL=49
来自 39.156.66.10 的回复: 字节=32 时间=24ms TTL=49
来自 39.156.66.10 的回复: 字节=32 时间=24ms TTL=49
39.156.66.10 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 24ms,最长 = 30ms,平均 = 25ms
Process finished with exit code 0
获取命令执行的状态码
import subprocess
test = subprocess.call("ping www.wuyupeng.com",shell=True,stdout=open("win32_null_file",mode="w"))
print(test)
0
2.shutil模块
复制文件
import shutil
shutil.copy(r"E:\projecttest\files\access.log",r"E:\projecttest\linux")
删除目录
递归删除目录下所有东西 相当于rm -rf
import shutil
shutil.rmtree(r"E:\projecttest\linux\python")
3.tarfile模块
文件打包压缩
import tarfile
tar_name = r"E:\projecttest\files\test.tar.gz"
tar_obj = tarfile.open(name=tar_name,mode="w:gz")
tar_obj.add(r"E:\projecttest\files\file01.txt")
tar_obj.add(r"E:\projecttest\IOmode.py")
4.hashlib模块
文件md5校验
要求对bytes格式进行加密
data = b"python md5 demo"
import hashlib
md5_obj = hashlib.md5()
md5_obj.update(data)
print(md5_obj.hexdigest())
3aae4b5d717336622979a6faa21d36a8
Process finished with exit code 0
#针对小文件
path = r"E:\projecttest\linux\access.txt"
import hashlib
obj = open(path,mode="rb")
data = obj.read()
print(type(data))
obj.close()
md5_data = hashlib.md5()
md5_data.update(data)
print(md5_data.hexdigest())
<class 'bytes'>
a76b4f7c9feef6f02ca90887e0e9ea25
Process finished with exit code 0
#通过循环一点一点往外都内容做校验,适合大文件
filename = r"E:\projecttest\linux\access.txt"
import hashlib
obj = open(filename,mode="rb")
md5test = hashlib.md5()
# data = obj.read(100)
# data1 = obj.read(10)
# print(data)
# print(data1)
while True:
data = obj.read(100)
#print(data)
if not data:
break
md5test.update(data)
print(md5test.hexdigest())
a76b4f7c9feef6f02ca90887e0e9ea25
Process finished with exit code 0
!!!hexdigest以下两种read加密后结果相同
updata更新暂存加密结果;最后hexdigest追加之前updata所有加密的结果 最后输出总结果。
path = r"E:\projecttest\linux\access.txt"
import hashlib
obj = open(path,mode="rb")
data = obj.read(100)
data1 = obj.read(100)
print(type(data))
obj.close()
md5_data = hashlib.md5()
md5_data.update(data)
md5_data.update(data1)
print(md5_data.hexdigest())
# path = r"E:\projecttest\linux\access.txt"
# import hashlib
#
# obj = open(path,mode="rb")
# data = obj.read(200)
#
# print(type(data))
# obj.close()
#
# md5_data = hashlib.md5()
# md5_data.update(data)
#
# print(md5_data.hexdigest())
5.pickle模块
数据持久化保存;存进去什么样的数据类型 以后输出的时候还是什么类型 直接打开是乱码 机器语言
pickle.dump存数据
# data = {"name":"tom","passwd":"123"}
#
# import pickle
# file_name = r"E:\projecttest\files\file02.txt"
# f_obj = open(file_name,mode="wb")
#
# pickle.dump(data,f_obj)
pickle.load读数据
import pickle
filename = r"E:\projecttest\files\file02.txt"
f_obj = open(filename,"rb")
data = pickle.load(f_obj)
print(type(data))
print(data)
f_obj.close()
如果按照之前的通过对象write写文件;类型是会被改变的
#定义一个字典
data = {"name":"wyp","passwd":"123"}
filename = r"E:\projecttest\files\file03.txt"
#提示写入必须是字符串格式 只能转为字符串
obj = open(filename,mode="w")
obj.write(str(data))
obj.close()
#读出来也是字符串 如果是rb模式就是bytes类型
obj_data = open(filename,"r")
hahaha = obj_data.read()
print(type(hahaha))
print(hahaha)
<class 'str'>
{'name': 'wyp', 'passwd': '123'}
Process finished with exit code 0
Comments NOTHING