Python os.popen() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.popen() method is used to open a pipe from a command.

Valid on Unix and Windows

Syntax

popen()The method syntax is as follows:

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

Parameters

  • command-- The command to use.

  • mode-- Mode permission can be 'r' (default) or 'w'.

  • bufsize-- Specifies the buffer size required for the file: 0 means unbuffered; 1 means line buffered; other positive values mean using a buffer of the parameter size (approximate value, in bytes). A negative bufsize means using the system default value. Generally, for tty devices, it is line buffered; for other files, it is fully buffered. If this parameter is not changed, the system default value is used.

Return Value

Returns an open file object with file descriptor fd

Example

The following example demonstrates the use of the popen() method:

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

import os, sys

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

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

print b

Executing the above program produces the following output:

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

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions