Home Backend Development Python Tutorial python调用shell的方法

python调用shell的方法

Jun 16, 2016 am 08:46 AM
shell command

1.1  os.system(command)

在一个子shell中运行command命令,并返回command命令执行完毕后的退出状态。这实际上是使用C标准库函数system()实现的。这个函数在执行command命令时需要重新打开一个终端,并且无法保存command命令的执行结果。

1.2  os.popen(command,mode)

打开一个与command进程之间的管道。这个函数的返回值是一个文件对象,可以读或者写(由mode决定,mode默认是'r')。如果mode为'r',可以使用此函数的返回值调用read()来获取command命令的执行结果。

1.3  commands.getstatusoutput(command)

使用os. getstatusoutput ()函数执行command命令并返回一个元组(status,output),分别表示command命令执行的返回状态和执行结果。对command的执行实际上是按照{command;} 2>&1的方式,所以output中包含控制台输出信息或者错误信息。output中不包含尾部的换行符。

2.1  subprocess.call(["some_command","some_argument","another_argument_or_path"])

subprocess.call(command,shell=True)

2.2  subprocess.Popen(command, shell=True)

如果command不是一个可执行文件,shell=True不可省。
使用subprocess模块可以创建新的进程,可以与新建进程的输入/输出/错误管道连通,并可以获得新建进程执行的返回状态。使用subprocess模块的目的是替代os.system()、os.popen*()、commands.*等旧的函数或模块。
最简单的方法是使用class subprocess.Popen(command,shell=True)。Popen类有Popen.stdin,Popen.stdout,Popen.stderr三个有用的属性,可以实现与子进程的通信。

将调用shell的结果赋值给python变量

复制代码 代码如下:

handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
print handle.communicate()[0]


在Python/wxPython环境下,执行外部命令或者说在Python程序中启动另一个程序的方法一般有这么几个:

1、os.system(command)

2、wx.Execute(command, syn=wx.EXEC_ASYNC, callback=None)

若置syn为wx.EXEC_ASYNC则wx.Excute函数立即返回,若syn=wx.EXEC_SYNC则等待调用的程序结束后再返回。

callback是一个wx.Process变量,如果callback不为None且syn=wx.EXEC_ASYNC,则程序结束后将调用wx.Process.OnTerminate()函数。

os.system()和wx.Execute()都利用系统的shell,执行时会出现shell窗口。如在Windows下会弹出控制台窗口,不美观。下面的两种方法则没有这个缺点。

3、class subprocess.Popen

最简单的用法是:

复制代码 代码如下:

import subprocess

subprocess.Popen(command, shell=True)

如果command不是一个可执行文件,shell=True不可省。

前面三个方法只能用于执行程序和打开文件,不能处理URL,打开URL地址可用webbrowser模块提供的功能。

4、webbrowser.open(url)

调用系统缺省浏览器打开URL地址,如 webbrowser.open('http://www.jb51.net'),也可以利用
webbrowser.open('h:\python.zip')来执行程序。这样可以不必区分是文件名还是URL,不知道在Linux下是否可行。
以上在Windows2000,Python2.4a1,wxPython 2.5.1运行。
modify:还有一种方式:subprocess.call(*args, **kwargs)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles