sys configparser random subprocess

与python解释器交互
import sys
print(sys.argv)#命令行参数List,第一个元素是程序本身路径
sys.exit(n) #退出程序,正常退出时exit(0)
print(sys.version) #获取Python解释程序的版本信息
sys.maxint #最大的Int值
print(sys.path) #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
print(sys.platform) # 返回操作系统平台名称
sys.stdout.write(‘please:’)

配置文件模块:用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
    config.write(configfile)

config.read('example.ini')#读取
print(config.sections())#块的信息 不包括default(读取时)
print(config.defaults())#默认项的内容
print(config.default_section)
print(config['bitbucket.org']['User'])

for key in config['topsecret.server.com']:
    print(key)

config.remove_section('topsecret.server.com')
config.has_section('topsecret.server.com')
config.add_section('topsecret.server.com')
config.set('bitbucket.org','user','ere')
config.remove_option('bitbucket.org','user')

config.write(open('i.cfg', "w"))# 改和删必须有

import random
print(random.random()) #0到1的随机数
print(random.randint(1,8))#包括右边8
print(random.choice(‘hello’))
print(random.choice([1,2,(5,6)]))
print(random.sample([‘23’,44,[3,4],77],2))#选多个
print(random.randrange(1,6))

#六位随机字母数字验证码
    def yan():
        import random
        code=''
    for i in range(6):
        x=random.choice([str(random.randrange(1,9)),chr(random.randrange(65,90))])#chr()将数字转换成ASCII码表的内容
        code+=x
    print(code)
yan()

如给运行的命令提供输入或者读取命令的输出,判断该命令的运行状态,管理多个命令的并行等等。这时subprocess中的Popen命令就能有效的完成我们需要的操作

# subprocess.Popen(),语法如下:
import subprocess
p = subprocess.Popen("find / -size +1000000 -exec ls -shl {} \;",shell=True,stdout=subprocess.PIPE)
print(p.stdout.read())
# 在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值