Python 2 到 Python 3 的变化

1: commands 被 subprocess 所取代:举例

  Python2中使用getoutput:

>>> import commands
>>> print(commands.getoutput('ls /usr/'))
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp

 

  Python3中使用getoutput:

>>> import subprocess
>>> print(subprocess.getoutput('ls /usr/'))
bin
etc
games
include
lib
lib64
libexec
local
sbin
share
src
tmp

2: Python2中的urllib被移植到Python3的urllib.request中:举例:

  Python2中使用urlopen:       

import urllib
url.urlopen("www.xxx.com")

  Python3中使用urlopen:

import urllib.request
url.request.urlopen("www.xxx.com")

 

posted @ 2017-12-25 13:44  Jansora  阅读(371)  评论(0编辑  收藏  举报