Python 基本语法规则

1. try:

try:
    tree = ET.parse(xmlFilePath)
     root = tree.getroot()
except Exception as e:
    logging.warning ("parse UserPasswordConfigure.xml:%s", e)
    sys.exit()

2. 一次性执行命令行

import os

command = 'ping www.baidu.com ' #可以直接在命令行中执行的命令

r = os.popen(command) #执行该命令

info = r.readlines()  #读取命令行的输出到一个list

for line in info:  #按行遍历

    line = line.strip('\r\n')

    print line

3. 管道

import subprocess

proc = subprocess.Popen('ping 192.168.6.77',stdout=subprocess.PIPE, shell=True)

while proc.poll() == None:
      text = proc.stdout.readline()
      print ("err:", proc.stderr.readline())

      if text == '':
          continue
      print ("zy:",i, text)
proc.wait()
print proc.returncode

猜你喜欢

转载自blog.csdn.net/u012206617/article/details/82426874