Python os.popen() method
Python OS file/directory methods
Overview
The os.popen() method opens a pipe from a command.
Valid on Unix and Windows
Syntax
The **popen()** method syntax is as follows:
os.popen(command[, mode[, bufsize]])
Parameters
-
command -- The command to use.
-
mode -- The mode can be 'r' (the default) or 'w'.
-
bufsize -- Specifies the buffer size needed for the file: 0 means unbuffered; 1 means line buffered; any other positive value means a buffer of roughly that size in bytes. A negative bufsize means the system default is used; generally for tty devices it is line buffered, and for other files it is fully buffered. If this parameter is not changed, the system default 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
# Use mkdir command
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