How can we interact with OS shell using Python ? I want to run windows cmd commands via python. How can it be achieved ?
我们如何使用Python与OS shell进行交互?我想通过python运行windows cmd命令。如何实现?
5 个解决方案
#1
44
The newer subprocess.check_output
and similar commands are supposed to replace os.system
. See this page for details. While I can't test this on Windows, the following should work:
较新的subprocess.check_output和类似命令应该替换os.system。有关详情,请参阅此页面。虽然我无法在Windows上测试,但以下应该可以正常工作:
from subprocess import check_output
check_output("dir C:", shell=True)
check_output
returns a string of the output from your command. Alternatively, subprocess.call
just runs the command and returns the status of the command (usually 0 if everything is okay).
check_output返回命令输出的字符串。或者,subprocess.call只运行命令并返回命令的状态(如果一切正常,通常为0)。
Also note that, in python 3, that string output is now bytes
output. If you want to change this into a string, you need something like
还要注意,在python 3中,该字符串输出现在是字节输出。如果你想把它改成一个字符串,你需要类似的东西
from subprocess import check_output
check_output("dir C:", shell=True).decode()
If necessary, you can tell it the kind of encoding your program outputs. The default is utf-8
, which typically works fine, but other standard options are here.
如有必要,您可以告诉它程序输出的编码类型。默认值为utf-8,通常可以正常工作,但此处还有其他标准选项。
#2
9
You would use the os module system method.
您将使用os模块系统方法。
You just put in the string form of the command, the return value is the windows enrivonment variable COMSPEC
你只需输入命令的字符串形式,返回值是windows enrivonment变量COMSPEC
For example:
例如:
os.system('python') opens up the windows command prompt and runs the python interpreter
os.system('python')打开windows命令提示符并运行python解释器
#3
6
Refactoring of @srini-beerge's answer which gets the output and the return code
重构@ srini-beerge的答案,获取输出和返回码
import subprocess
def run_win_cmd(cmd):
result = []
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for line in process.stdout:
result.append(line)
errcode = process.returncode
for line in result:
print(line)
if errcode is not None:
raise Exception('cmd %s failed, see above for details', cmd)
#4
0
import subprocess
result = []
win_cmd = 'ipconfig'(curr_user,filename,ip_address)
process = subprocess.Popen(win_cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE )
for line in process.stdout:
print line
result.append(line)
errcode = process.returncode
for line in result:
print line
#5
0
You can use the subprocess
package with the code as below:
您可以将子进程包与代码一起使用,如下所示:
import subprocess
cmdCommand = "python test.py" #specify your cmd command
process = subprocess.Popen(cmdCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print output
#1
44
The newer subprocess.check_output
and similar commands are supposed to replace os.system
. See this page for details. While I can't test this on Windows, the following should work:
较新的subprocess.check_output和类似命令应该替换os.system。有关详情,请参阅此页面。虽然我无法在Windows上测试,但以下应该可以正常工作:
from subprocess import check_output
check_output("dir C:", shell=True)
check_output
returns a string of the output from your command. Alternatively, subprocess.call
just runs the command and returns the status of the command (usually 0 if everything is okay).
check_output返回命令输出的字符串。或者,subprocess.call只运行命令并返回命令的状态(如果一切正常,通常为0)。
Also note that, in python 3, that string output is now bytes
output. If you want to change this into a string, you need something like
还要注意,在python 3中,该字符串输出现在是字节输出。如果你想把它改成一个字符串,你需要类似的东西
from subprocess import check_output
check_output("dir C:", shell=True).decode()
If necessary, you can tell it the kind of encoding your program outputs. The default is utf-8
, which typically works fine, but other standard options are here.
如有必要,您可以告诉它程序输出的编码类型。默认值为utf-8,通常可以正常工作,但此处还有其他标准选项。
#2
9
You would use the os module system method.
您将使用os模块系统方法。
You just put in the string form of the command, the return value is the windows enrivonment variable COMSPEC
你只需输入命令的字符串形式,返回值是windows enrivonment变量COMSPEC
For example:
例如:
os.system('python') opens up the windows command prompt and runs the python interpreter
os.system('python')打开windows命令提示符并运行python解释器
#3
6
Refactoring of @srini-beerge's answer which gets the output and the return code
重构@ srini-beerge的答案,获取输出和返回码
import subprocess
def run_win_cmd(cmd):
result = []
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for line in process.stdout:
result.append(line)
errcode = process.returncode
for line in result:
print(line)
if errcode is not None:
raise Exception('cmd %s failed, see above for details', cmd)
#4
0
import subprocess
result = []
win_cmd = 'ipconfig'(curr_user,filename,ip_address)
process = subprocess.Popen(win_cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE )
for line in process.stdout:
print line
result.append(line)
errcode = process.returncode
for line in result:
print line
#5
0
You can use the subprocess
package with the code as below:
您可以将子进程包与代码一起使用,如下所示:
import subprocess
cmdCommand = "python test.py" #specify your cmd command
process = subprocess.Popen(cmdCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print output