在Python 3.0 a1 中,对外部程序的调用和原来的版本有所区别。在Python 2.x 时代,调用外部程序时在Unix和Window下需要分别"import os" 或"import win32pipe"。现在2个模块已经统一,只需要"import os"就可以了。
以下代码是在Windows下,使用Python 3.0 调用"dir"并输出"c:/"下的列表。
#
-*- coding: utf-8 -*-
import os,string # 载入os string模块
scmd = " dir c:/ " # 执行命令dir c:
sdir = os.popen(scmd).read() # 读取返回结果
print ( " In Python. " )
print (sdir) # 输出返回结果
print ( " Out Python. " )
import os,string # 载入os string模块
scmd = " dir c:/ " # 执行命令dir c:
sdir = os.popen(scmd).read() # 读取返回结果
print ( " In Python. " )
print (sdir) # 输出返回结果
print ( " Out Python. " )
在3.0的代码中"print" 已经变为了"print()"。
最后执行结果如下。
D:
pythondev
>
python listc
.
py
In Python .
驱动器 C 中的卷是 server
卷的序列号是 0830 - 0152
c: 的目录
2007 - 06 - 20 23 : 25 0 AUTOEXEC . BAT
2007 - 11 - 02 10 : 52 20 , 832 bar . emf
2007 - 11 - 06 13 : 34 26 , 624 BS . xlt
2007 - 06 - 20 23 : 25 0 CONFIG . SYS
2007 - 06 - 20 23 : 36 < DIR > Documents and Settings
2007 - 11 - 06 09 : 31 17 , 920 PL . xlt
2007 - 10 - 23 13 : 30 < DIR > Program Files
2007 - 09 - 28 14 : 29 125 Protocol . txt
2007 - 10 - 23 09 : 50 < DIR > Python30
2007 - 09 - 18 14 : 07 < DIR > TEMP
2007 - 11 - 01 16 : 07 < DIR > testlog
2007 - 11 - 15 08 : 48 < DIR > WINDOWS
2007 - 06 - 20 23 : 27 < DIR > wmpub
6 个文件 65 , 501 字节
7 个目录 5 , 254 , 881 , 280 可用字节
Out Python .
In Python .
驱动器 C 中的卷是 server
卷的序列号是 0830 - 0152
c: 的目录
2007 - 06 - 20 23 : 25 0 AUTOEXEC . BAT
2007 - 11 - 02 10 : 52 20 , 832 bar . emf
2007 - 11 - 06 13 : 34 26 , 624 BS . xlt
2007 - 06 - 20 23 : 25 0 CONFIG . SYS
2007 - 06 - 20 23 : 36 < DIR > Documents and Settings
2007 - 11 - 06 09 : 31 17 , 920 PL . xlt
2007 - 10 - 23 13 : 30 < DIR > Program Files
2007 - 09 - 28 14 : 29 125 Protocol . txt
2007 - 10 - 23 09 : 50 < DIR > Python30
2007 - 09 - 18 14 : 07 < DIR > TEMP
2007 - 11 - 01 16 : 07 < DIR > testlog
2007 - 11 - 15 08 : 48 < DIR > WINDOWS
2007 - 06 - 20 23 : 27 < DIR > wmpub
6 个文件 65 , 501 字节
7 个目录 5 , 254 , 881 , 280 可用字节
Out Python .
在In Python和Out Python之间的内容就为执行结果