configparser模块和subprocess模块

configparser模块

该模块适用于配置文件,配置文件类似于windows的ini文件相似。可以包含一个或多个节(section)。

导入该模块

import configparser

创建对象实例

cfg=configparser.ConfigParser()
cfg[‘DEFAULT‘]={‘backgroud‘:‘green‘,‘font‘:‘20‘,‘color‘:‘red‘,‘width‘:‘30‘,‘height‘:‘50‘}
cfg[‘charactor‘]={‘actor‘:‘yuang‘,‘actress‘:‘mengmeng‘,‘editor‘:‘zhangjiao‘}
cfg[‘price‘]={‘phone‘:‘100‘,‘computer‘:‘3000‘,‘cup‘:10}  #写入文件
with open(‘base.ini‘,‘w‘) as f:    #保存为文件
    cfg.write(f)
cfg=configparser.ConfigParser()
cfg.read(‘base.ini‘)    #读取ini文件
print(cfg.sections())     #[‘charactor‘, ‘price‘]
print(cfg.options(section=‘charactor‘))#取出[‘actor‘, ‘actress‘, ‘editor‘, ‘backgroud‘, ‘font‘, ‘color‘, ‘width‘, ‘height‘]
for i in cfg.values():
    print(i)        #<Section: DEFAULT> <Section: charactor> <Section: price>
print(cfg[‘price‘][‘phone‘]) #100

subprocess模块

当我们需要调用系统的命令的时候,最先考虑的os模块。用os.system()和os.popen()来进行操作。但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出,判断该命令的运行状态,管理多个命令的并行等等。这时subprocess中的Popen命令就能有效的完成我们需要的操作。

subprocess模块允许一个进程创建一个新的子进程,通过管道连接到子进程的stdin/stdout/stderr,获取子进程的返回值等操作。

该模块只有一个类Popen


导入该模块

import subprocess
s=subprocess.Popen(‘dir‘,shell=True,stdout=subprocess.PIPE)
print(s.stdout.read().decode(‘gbk‘))

在linux系统下可以不用加shell=True

使用ls -l 的命令时要加shell=True

也可以使用subprocess.Popen([‘ls‘,‘-l‘])

subprocess.Popen(‘ls -l‘,shell=True)

时间: 2024-08-25 03:21:09

configparser模块和subprocess模块的相关文章

python重要模块之subprocess模块

python重要模块之subprocess模块 我们经常要通过python去执行系统的命令或者脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就相当于发起了一个新的进程,通过python调用系统命令或脚本的模块. 之前我们也学到过和系统交互的模块-os模块 In [1]: import os In [2]: os.system('uname -a') Linux host-10-200-137-195 3.10.0-693.21.1.el7.x86_64 #1 SM

Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块

正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): 6 page_string=requests.get(url) 7 return page_string.text 8 9 hua_dic={} 10 def run_re(url): #爬取名字.学校和喜爱的人数 11 hua_str=getPage_str(url) 12 hua_list=r

处理xml模块、configparser模块、hashlib模块、subprocess模块

xml模块 新建a.xml内容为: <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year updated="yes" version="1.0">2009</year> <gdppc>141100</gdppc> <neighbor d

Python基础----正则表达式爬虫应用,configparser模块和subprocess模块

正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): 6 page_string=requests.get(url) 7 return page_string.text 8 9 hua_dic={} 10 def run_re(url): #爬取名字.学校和喜爱的人数 11 hua_str=getPage_str(url) 12 hua_list=r

os模块、os.path模块、shutil模块、configparser模块、subprocess模块

一.os模块 os指的是错作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd()  获取当前文件所在的文件夹路径 os.chdir()  修改当前工作目录 os.makedirs()  创建多级目录(不存在时,则创建) os.mkdir()  创建单级目录 os.rmdir()  删除文件夹(文件夹为空,则删除) os.listdir()  列出指定目录下的所有文件及子目录(存在一个列表中) os.stat()  获取文件信息 os

Python configparser模块 与 subprocess 模块

configparser 模块 Python中 configparser 模块用于读取和编辑配置文件,更多的是用于读取配置文件.配置文件的格式如下,可以包含多个section(例如:db,email),每个section又可以有多个键值对(例如:database=bps):其中 '=' 也可以使用 ':' 取代~ [default] log_path=/tmp/csv.log [db] host=192.168.1.20 database=bps user=bps password=123456

python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】

.title { background-color: #000000; color: #00f508 } 一.shelve模块 1 import shelve 2 3 # 基于pickle模块, 4 5 d = shelve.open('shelve_test') 6 7 8 class Test(object): 9 def __init__(self, n): 10 self.n = n 11 12 t1 = Test(123) 13 t2 = Test(456) 14 name = ['a

re模块、os模块、subprocess模块

一.re模块 1.什么是正则 正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串, 正则就是用来去一个大的字符串中匹配出符合规则的子字符串 2.为什么要用正则 1.用户注册 2.爬虫程序 3.如何用正则 # =================================匹配模式================================= #一对一的匹配 # 'hello'.replace(old,new) # 'hello'.find('pattern'

python模块之subprocess模块, struct模块

subprocess import subprocess ''' sh-3.2# ls /Users/egon/Desktop |grep txt$ mysql.txt tt.txt 事物.txt ''' res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE) res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout, s