《Python核心编程》第14章 执行环境 练习
2014-06-03 20:19
302 查看
14-3.执行环境。
创建运行其他Python脚本的脚本。
filename = raw_input('file name: ')
execfile(filename)14-4. os.system()。
调用os.system()运行程序。附加题:将你的解决方案移植到subprocess.call()。
import os
from subprocess import call
os.system('dir')
call('cmd /c dir')14-5. commands.getoutput()。
用commands.getoutput()解决前面的问题。
from commands import getoutput
output = getoutput('ls')
print output14-6.popen()家族。
选择熟悉的系统命令,该命令从标准输入获得文本,操作或输出数据。使用os.popen()与程序进行通信。
from os import popen
f = popen('dir')
for ch in f:
print ch,14-7.subprocess模块。
把先前问题的解决方案移植到subprocess模块。
from subprocess import check_output
ret = check_output('cmd /c dir')
print ret14-8.exit函数。
设计一个在程序退出时的函数,安装到sys.exitfunc(),运行程序,演示你的exit函数确实被调用了。
import sys
def foo():
print 'show message'
sys.exitfunc = foo
print '123'14-9.Shells.
创建shell(操作系统接口)程序。给出接受操作系统命令的命令行接口(任意平台)。
import os
while True:
cmd = raw_input('$: ')
if cmd == 'exit':
break
f = os.popen(cmd)
for line in f:
print line,
print
创建运行其他Python脚本的脚本。
filename = raw_input('file name: ')
execfile(filename)14-4. os.system()。
调用os.system()运行程序。附加题:将你的解决方案移植到subprocess.call()。
import os
from subprocess import call
os.system('dir')
call('cmd /c dir')14-5. commands.getoutput()。
用commands.getoutput()解决前面的问题。
from commands import getoutput
output = getoutput('ls')
print output14-6.popen()家族。
选择熟悉的系统命令,该命令从标准输入获得文本,操作或输出数据。使用os.popen()与程序进行通信。
from os import popen
f = popen('dir')
for ch in f:
print ch,14-7.subprocess模块。
把先前问题的解决方案移植到subprocess模块。
from subprocess import check_output
ret = check_output('cmd /c dir')
print ret14-8.exit函数。
设计一个在程序退出时的函数,安装到sys.exitfunc(),运行程序,演示你的exit函数确实被调用了。
import sys
def foo():
print 'show message'
sys.exitfunc = foo
print '123'14-9.Shells.
创建shell(操作系统接口)程序。给出接受操作系统命令的命令行接口(任意平台)。
import os
while True:
cmd = raw_input('$: ')
if cmd == 'exit':
break
f = os.popen(cmd)
for line in f:
print line,
相关文章推荐
- Dave Python 练习十六 -- 执行环境
- Dave Python 练习十六 -- 执行环境
- ecma262-3执行环境练习
- Dave Python 练习十六 -- 执行环境
- Dave Python 练习十六 -- 执行环境
- Python核心编程---读书笔记:第14章 执行环境
- Python核心编程(第十四章)--执行环境
- Dave Python 练习十六 -- 执行环境
- Python核心编程读笔 13:执行环境
- 自由软件来生成纯粹的Win32可执行文件的编译环境
- PB的可执行文件所需的环境DLL
- .Net Framework的公共语言执行环境(CLR)
- 修改SQL Server 2005执行环境
- 在类库内部判断代码执行环境
- 利用statspack来获取生成环境中top SQL及其执行计划
- 修改SQL Server 2005执行环境
- Oracle 9i & 10g编程艺术-深入数据库体系结构——第14章:并行执行
- 判断程序的执行环境是VM
- 判断程序的执行环境是VM
- HOW TO:获取执行代码所处环境信息(应用)