Python os.popen() 方法

Python 教程 · 2019-02-04 09:11:46

概述

os.popen() 方法用于从一个命令打开一个管道。

在Unix,Windows中有效

语法

popen()方法语法格式如下:

os.popen(command[, mode[, bufsize]])

参数

  • command -- 使用的命令。

  • mode -- 模式权限可以是 'r'(默认) 或 'w'。

  • bufsize -- 指明了文件需要的缓冲大小:0意味着无缓冲;1意味着行缓冲;其它正值表示使用参数大小的缓冲(大概值,以字节为单位)。负的bufsize意味着使用系统的默认值,一般来说,对于tty设备,它是行缓冲;对于其它文件,它是全缓冲。如果没有改参数,使用系统的默认值。

返回值

返回一个文件描述符号为fd的打开的文件对象

实例

以下实例演示了 popen() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 使用 mkdir 命令
a = 'mkdir nwdir'

b = os.popen(a,'r',1)

print b

执行以上程序输出结果为:

open file 'mkdir nwdir', mode 'r' at 0x81614d0

点击查看所有 Python 教程 文章: https://codercto.com/courses/l/8.html

查看所有标签

XML Hacks

XML Hacks

Michael Fitzgerald / O'Reilly Media, Inc. / 2004-07-27 / USD 24.95

Developers and system administrators alike are uncovering the true power of XML, the Extensible Markup Language that enables data to be sent over the Internet from one computer platform to another or ......一起来看看 《XML Hacks》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具