LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

Python简单技巧和常用参考

时间:2015-01-18 12:42来源:opstool.com 编辑:opstool 点击:
python文件支持中文 # -*- coding: UTF-8 -*- 执行shell命令 from subprocess import Popen, PIPEdef run_cmd(cmd): #Popen call wrapper.return (code, stdout, stderr) child = Popen(cmd, stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = T
python文件支持中文
# -*- 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

------分隔线----------------------------
标签:Python
栏目列表
推荐内容