configparser模块
# configparser用于处理特定格式的文件,其本质上是利用open来操作文件
# 下边我们就创建这种特定格式配置文件,来操作以下这里模块方法
--------------test.conf----------------
[section1] # configparser 会认定以中括号括住的为一个节点(node)
k1 = 111 # 节点下,每一行配置文件为键值对存在(也可以写成 k2:123)
k2 = v2
k3 = 123
k4 = True
k10 = 100
[section2]
k1 = v1
# 封装一个对象
config = configparser.ConfigParser()
# 获取所有节点(sections方法)
config.read('test.conf',encoding='utf-8')
ret = config.sections() # 获取所有使用中括号的节点名称
print(ret)
==> ['section1', 'section2'] # section方法返回一个列表,包含所有节点名称
# 获取指定节点下所有的键值对(items方法)
ret = config.items('section1') # 获取文件的 key = v 或 key:v 等 键值对
print(ret)
==> [('k1', '123'), ('k2', 'v2'), ('k3', '123'), ('k4', 'True'), ('k10', '100')]
# 获取指定节点下所有的键(options方法)
ret = config.options('section1')
print(ret)
==> ['k1', 'k2', 'k3', 'k4', 'k10']
# 获取指定节点下指定key的值 (get方法)
# 注意,下边强制转换不成功是会报错的
ret1 = config.get('section1', 'k1') # 获取section节点下的k1键对应的值
ret2 = config.getint('section1', 'k3') # 获取section1节点下k3键对应的值,并将该值转换为Int类型
ret3 = config.getfloat('section1', 'k3') # 获取section1节点下k3键对应的值,并将该值转换为float类型
ret4 = config.getboolean('section1', 'k4') # 获取section1节点下k4键对应的值,并将该值转换为boolean类型
print(ret1,ret2,ret3,ret4)
==> 111 123 123.0 True
### 检查、删除、添加节点 ###
# 检查节点(has_section方法)
has_sec = config.has_section('section1') # 检查是否有section1节点
print(has_sec)
==> True # 返回boolean值
# 添加节点 (add_section方法)
config.add_section('SEC_1') # 添加SEC_1节点
config.write(open('test.conf', 'w')) # 将修改的内容写入到test.conf
# 删除节点 (remove_section方法)
config.remove_section('SEC_1') # 删除SEC_1节点
config.write(open('test.conf', 'w')) # 将修改的内容写入到test.conf
### 检查、删除、设置指定组内的键值对 ###
# 检查键值对 (has_option方法)
has_opt = config.has_option('section1', 'k1') # 检查section1节点是否有k1键
print(has_opt)
# 删除键值对 (remove_option方法)
config.remove_option('section1', 'k1') # 删除section1节点的k1键对应的键值对(这一行)
config.write(open('test.conf', 'w')) # 将修改的内容写入到test.conf
# 设置键值对 (set方法)
config.set('section1', 'k10', "123") # 设置(有修改,无添加)section1节点,k10=123
config.write(open('test.conf', 'w')) # 将修改的内容写入到test.conf
# 注意,我们配置文件被读取后,放入内存中
# 我们刚刚所做的一切的增删改,都是操作的内存中已加载的配置文件,
# 并没有对实际文件有所改变,所以我们需要执行write方法写入配置文件中
shutil
# 将文件内容拷贝到另一个文件中
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
# 拷贝文件
shutil.copyfile('f1.log', 'f2.log')
# 拷贝文件和权限
shutil.copy('f1.log', 'f2.log')
# 递归的去拷贝文件夹,并忽略拷贝pyc结尾,和tmp开头的文件
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
# 递归的去删除文件
shutil.rmtree('folder1')
# 递归的去移动文件,它类似mv命令,其实就是重命名。
shutil.move('folder1', 'folder3')
压缩与解压(zipfile、tarfile)
# 压缩解压 zip
import zipfile
# 压缩
z = zipfile.ZipFile('test.zip','w') # 创建一个 以写的方式写入test.zip 的对象
z.write('xml_t2.xml') # 添加xml_t2.xml文件到test.zip
z.write('xml_t3.xml') # 添加xml_t3.xml文件到test.zip
z.close()
# 解压
z = zipfile.ZipFile('test.zip', 'r')
z.extractall() # 解压所有zip
# z.extract() # 指定解压单个
z.close()
# 压缩解压 tarfile
import tarfile
# 压缩
tar = tarfile.open('your.tar', 'w') # 创建一个 以写的方式写入test.tar 的对象
tar.add('xml_t2.xml',arcname='xml2.xml') # 添加tar,并重命名xml2.xml
tar.add('xml_t3.xml',arcname='xml3.xml')
tar.close()
# 解压
tar = tarfile.open('your.tar', 'r')
tar.extractall() # 解压全部,并可设置解压地址
tar.close()
subprocess
# call 执行命令,返回状态码
ret = subprocess.call('dir', shell=True)
ret = subprocess.call(['ipconfig','/all'], shell=False) # shell为False,需要传入列表
# check_call 执行命令,如果执行状态码
ret = subprocess.check_call(['ipconfig','/all'])
ret = subprocess.check_call('exit 1',shell=True) # 没有exit命令,会抛异常
# check_output 执行命令,如果状态码是0,则返回执行结果,否则抛异常
subprocess.check_output(['echo', 'hello world'])
subprocess.check_output('echo hello', shell=True)
# popen 方法
obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',) # 跳转到/home/dev,执行命令
# popen 管道使用
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # 指定stdin、stdout、stderr流
obj.stdin.write("print(1)\n") # 写入print(1) 的stdin流
obj.stdin.write("print(2)")
obj.stdin.close()
cmd_out = obj.stdout.read() # 读取stdout 数据
obj.stdout.close()
cmd_error = obj.stderr.read() # 读取stderr数据
obj.stderr.close()
# popen 读取管道另一种方法
out_err_list = obj.communicate() # 生成一个列表,列表包含stdout和stderr数据,是一个综合方法
print(cmd_out)
print(cmd_error)