1. 程式人生 > >Python 基本語法規則

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