python subprocess

本文介绍了使用Python通过os模块及subprocess模块来调用外部程序的方法。包括os.system与os.popen的不同用法,以及推荐使用的subprocess模块的call与Popen函数。文章提供了具体的代码示例,并附带相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os

os.system('etc/program') #full path of the program

os.system('gedit')

#os.system('notpad.exe') #for windows

 

or:

os.popen()

 

os.popen() behaves similar to a file open(), the default is "r" for read, so for write to have to addd a "w". You can specify the buffer size in bytes or use the default of zero.

 

os.popen('program').write('whatever')

 

a = os.popen("cmd","w")

a.write("bla")

 

some discussion here: http://www.daniweb.com/forums/thread40790.html

see more here: http://docs.python.org/library/os.html

 

also: http://effbot.org/librarybook/os.htm

 

*******************

Use another method, subprocess, which is recommended. http://docs.python.org/library/subprocess.html

also http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html

 

import subprocess# Command with shell expansion
subprocess.call('ls -l $HOME', shell=True)

subprocess.Popen('echo "Hello world!"', shell=True) #command through the shell.

 

 

 

This blog is also very great: http://jimmyg.org/blog/2009/working-with-python-subprocess.html

 

 

 

 

###################################################

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值