1.Python的commands模块在3.x版本中,getstatus方法被移除,getoutput和getstatusoutput被放到了subprocess模块中
py2的commands.getstatusoutput修改兼容py2和py3
import commands
status, stdout = commands.getstatusoutput("pwd")
print(status)
print(stdout)
方法1:使用subprocess.getstatusoutput
try:
import commands
except ImportError:
import subprocess as commands
status, stdout = commands.getstatusoutput("pwd")
print(status)
print(stdout)
方法2:使用subprocess.Popen
st = subprocess.Popen('pwd', stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).stdout.readline()
print(bytes.decode(st))
2.py2的urlparse.urljoin
if sys.version.major == 3:
from urllib.parse import urljoin
url = urljoin("url1", "url2")
else:
import urlparse
url = urlparse.urljoin("url1", "url2")
3.py2 execfile
file = ""
if os.path.exists(file):
if sys.version.major == 2:
execfile(file, dict(__file__=file))
else:
with open(file, 'r', encoding='utf-8') as ff:
exec(ff.read())
|