configparser 模块

功能:操作模块类的文件,configparser类型文件的操作类似于字典,大多数用法和字典相同。

新建文件:

import configparser
cfg=configparser.ConfigParser()
cfg['DEFAULT']={'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': '',
'ForwardX11':'yes'
}
cfg['bitbucket.org']={'User':'hg'}
cfg['topsecret.server.com']={'host port':'','Forwardx11':'no'}
with open('cfg.int','w') as f:
cfg.write(f)
#DEFAULT 关键字,是默认参数,将DEFAULT 里的内容(value)匹配给每个自定义模块,比如:bitbucket.org

cfg.int 文件内容如下:

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no 操作文件内容
import configparser

config = configparser.ConfigParser()

#---------------------------查找文件内容,基于字典的形式

print(config.sections())        #  []

config.read('example.ini')

print(config.sections())        #   ['bitbucket.org', 'topsecret.server.com']

print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key) print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键 print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对 print(config.get('bitbucket.org','compression')) # yes get方法取深层嵌套的值

增删改操作

import configparser

config = configparser.ConfigParser()

config.read('example.ini')

config.add_section('yuan')

config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11") config.set('topsecret.server.com','k1','')
config.set('yuan','k2','') config.write(open('new2.ini', "w"))

subprocess模块

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

import subprocess

#  创建一个新的进程,与主进程不同步  if in win: s=subprocess.Popen('dir',shell=True)
s=subprocess.Popen('ls')
s.wait() # s是Popen的一个实例对象 print('ending...')

子进程文本控制流

可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):

import subprocess

# s1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
# print(s1.stdout.read()) #s2.communicate() s1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
s2 = subprocess.Popen(["grep","0:0"],stdin=s1.stdout, stdout=subprocess.PIPE)
out = s2.communicate() print(out)
												

常用模块-----configparser & subprocess的更多相关文章

  1. python常用模块之subprocess

    python常用模块之subprocess python2有个模块commands,执行命令的模块,在python3中已经废弃,使用subprocess模块来替代commands. 介绍一下:comm ...

  2. Python全站之路----常用模块----configparser模块

    config:配置    parser:解析 此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser,在 python 2.x 里名字为 Co ...

  3. Python常用模块--configparser

    作用: 官方:实现基本配置语言的类,该语言提供类似于Microsoft Windows INI文件中的结构.您可以使用它来编写可由最终用户轻松定制的Python程序. 通俗的说:该模块用于系统配置文件 ...

  4. python之常用模块ConfigParser

    这个常见于.conf,.ini等类型的配置文件 下面先看一下如果通过python生成一个.ini文件 import configparser #2.x is ConfigParserconfig = ...

  5. python常用模块及面向对象(一)

    目录: 常用模块之time模块 常用模块之random模块 常用模块之os模块 常用模块之sys模块 常用模块之subprocess模块 常用模块之json模块 常用模块之pickle模块 常用模块之 ...

  6. python常用模块集合

    python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...

  7. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

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

    一.os模块 os指的是操作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd()  获取当前文件所在的文件夹路径 os.chdir()  ...

  9. os模块,os.path模块,subprocess模块,configparser模块,shutil模块

    1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...

随机推荐

  1. 关于fork()父子进程返回值的问题

    我们都知道,父进程fork()之后返回值为子进程的pid号,而子进程fork()之后的返回值为0.那么,现在就有一个问题了,子进程fork()的返回值是怎么来的?如果子进程又执行了一遍fork()函数 ...

  2. Android startActivity()和onActivityResult()使用总结(转载)

    有三个Activity: A.java ,B.java ,C.java Activity之间的跳转常用方法: 1. startActivity(Intent intent); 该方法只用于启动新的Ac ...

  3. OC 内存管理-02 autorelease 概念 以及用法

    (1) @autoreleasepool { }//自动释放池代表,池子将要被销毁,对池子中所有的对象进行一次release操作 (2) 不管你这个对象时在@autoreleasepool 之内创建的 ...

  4. javascript对象定义

    转载自:http://blog.sina.com.cn/s/blog_75a8cfac0100pif0.html javascript定义对象写法 javascript定义对象的几种简单方法 1.构造 ...

  5. 冒泡排序-python

    题目: 如果一个list是一组打乱的数字 list1=[3,2,1,9,10,78,6] 如何用python将这组打乱的数字进行冒泡排序? 题解: def sort(nums): for i in r ...

  6. <转载> Jquery的使用技巧-实用!

    1.使用方法在需要使用JQuery的页面中引入JQuery的js文件即可.例如:<script type="text/javascript" src="js/jqu ...

  7. CGI FastCGI PHP-CGI PHP-FRM

    CGI(Common GateWay Interface )通用网关接口,CGI可以让一个客户端,从网页浏览器向执行在Web服务器上的程序请求数据.CGI描述了客户端和这个程序之间传输数据的一种协议标 ...

  8. 41、Android中当数据库需要更新时我们该怎么办?

    转载  http://blog.csdn.net/jiangwei0910410003/article/details/39670813

  9. splay tree成段更新,成段查询poj3466

    线段树入门题,换成splay tree 来搞搞. #include <stdio.h> #include <string.h> #include <algorithm&g ...

  10. 使用RestTemplate post方式提交表单数据

    HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODE ...