当前位置:首页>开发>正文

python脚本中怎么调用shell命令 windows python 能调用shell命令吗

2023-04-12 14:18:00 互联网 未知 开发

 python脚本中怎么调用shell命令 windows python 能调用shell命令吗

python脚本中怎么调用shell命令

os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等待返回。返回值是依赖于系统的,直接返回系统的调用返回值,所以windows和linux是不一样的
os.popen(command[,mode[,bufsize]]),图中是一个例子. 可以看出,popen方法通过p.read()获取终端输出,而且popen需要关闭close().当执行成功时,close()不返回任何值,失败时,close()返回系统返回值. 可见它获取返回值的方式和os.system不同。

使用commands模块,图中是一组例子。根据你需要的不同,commands模块有三个方法可供选择。getstatusoutput, getoutput, getstatus。

windows python 能调用shell命令吗

最近有个需求就是页面上执行shell命令,第一想到的就是os.system,

复制代码 代码如下:

os.system(cat /proc/cpuinfo)

但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()

复制代码 代码如下:

output = os.popen(cat /proc/cpuinfo)
print output.read()

通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。

复制代码 代码如下:

(status, output) = commands.getstatusoutput(cat /proc/cpuinfo)
print status, output

Python Document 中给的一个例子,

复制代码 代码如下:

>>> import commands
>>> commands.getstatusoutput(ls /bin/ls)
(0, /bin/ls)
>>> commands.getstatusoutput(cat /bin/junk)
(256, cat: /bin/junk: No such file or directory)
>>> commands.getstatusoutput(/bin/junk)
(256, sh: /bin/junk: not found)
>>> commands.getoutput(ls /bin/ls)
/bin/ls
>>> commands.getstatus(/bin/ls)
-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls

最后页面上还可以根据返回值来显示命令执行结果。

请教python如何执行shell管道命令

Python执行Linux系统命令,即在Python脚本中调用Shell命令,具体有以下四种方法:
1、os.system
//仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
//如果再命令行下执行,结果直接打印出来:
>>> os.system(ls)
04101419778.CHM bash document media py-django video
11.wmv books downloads Pictures python
all-20061022 Desktop Examples project tools
2、os.popen
//该方法不但执行命令还返回执行后的信息对象
popen(command [, mode=r [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.
3、使用模块 subprocess
>>> import subprocess
>>> subprocess.call(["cmd", "arg1", "arg2"],shell=True)
//获取返回和输出:
import subprocess
p = subprocess.Popen(ls, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
4、使用模块 commands

>>> import commands
>>> dir(commands)
[__all__, __builtins__, __doc__, __file__, __name__, getoutput, getstatus,getstatusoutput, mk2arg, mkarg]
>>> commands.getoutput("date")
Wed Jun 10 19:39:57 CST 2009
>>>
>>> commands.getstatusoutput("date")
(0, Wed Jun 10 19:40:41 CST 2009)

如何直接在pythonshell中运行程序

在windows下的cmd窗口中执行Python程序,我一般是这样做:
1. 执行一个reg文件,内容为:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT/Directory/shell/cmd/command]
@="cmd.exe /k /"cd %L/""
它的作用是在你的资源管理器上的右键菜单上增加一个菜单,名字为cmd。那么以后你在目录栏中点击一个目录,然后点右键,再执行这个cmd菜单,就会直接进入这个目录的命令行。
2. 因为已经进入了你的python程序所在的目录,因此直接在命令行下运行:
Python yourprog.py
即可。
前提是你已经将 Python 的安装目录加到 PATH 的环境变量中。

python shell怎么打开

输入命令
# python
出现下面内容,即进入python shell,可以写代码了
Python 2.7.5 (default, Nov 3 2014, 14:33:39)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linuxType "help", "copyright", "credits" or "license" for more information.
>>>

最新文章