Python3 os.popen() Method

Python3 OS 文件/目录方法Python3 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 permissions can be 'r' (default) or 'w'.

  • bufsize-- Specifies the buffering size required for the file: 0 means unbuffered; 1 means line buffered; other positive values indicate buffering using a buffer of the parameter's size (approximate value, in bytes). Negative bufsize means using the system default. In general, for tty devices, it is line buffered; for other files, it is fully buffered. If this parameter is not provided, the system default is used.

Return Value

Returns an open file object with file descriptor fd.

Examples

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

#!/usr/bin/python3

import os, sys

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

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

print (b)

The output of the above program is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions