# -*- coding: UTF-8 -*- 执行shell命令 from subprocess import Popen, PIPE def run_cmd(cmd): #Popen call wrapper.return (code, stdout, stderr) child = Popen(cmd, stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = True) out, err = child.communicate() ret = child.wait() return (ret, out, err) 获取当前python脚本文件所在路径 import os os.path.split(os.path.realpath(__file__))[0] json模块import的问题 try : import json except : import simplejson as json 使用json工具格式化json #python 2.7以下 echo '{"hello":1}' | python -m simplejson.tool #python 2.7及以上 echo '{"hello":1}' | python -m json.tool 获取URL资源 import urllib2 response = urllib2.urlopen('http://www.opstool.com/') html = response.read() print html 关于main if __name__=='__main__': 转载请保留固定链接: https://linuxeye.com/program/2104.html |