Python简单技巧和常用参考,python技巧,python文件支持中文


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 = True)    out, err = child.communicate()    ret = child.wait()    return (ret, out, err)

获取当前python脚本文件所在路径

import osos.path.split(os.path.realpath(__file__))[0]

json模块 import的问题

try :    import jsonexcept :    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 urllib2response = urllib2.urlopen('http://www.opstool.com/')html = response.read()print html

关于main

if __name__=='__main__':

评论关闭