Python3 os.popen() Method
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
The syntax for the popen()
method is as follows:
os.popen(command[, mode[, bufsize]])
Parameters
-
command -- The command to be used.
-
mode -- The mode permission can be 'r' (default) or 'w'.
-
bufsize -- Specifies the buffer size needed for the file: 0 means no buffering; 1 means line buffering; other positive values indicate the buffer size (approximate value, in bytes). Negative bufsize means using the system default, which is generally line buffering for tty devices and fully buffered for other files. If this parameter is not provided, the system default is used.
Return Value
Returns an open file object with the file descriptor fd.
Example
The following example demonstrates the use of the popen()
method:
#!/usr/bin/python3
import os, sys
# Using the mkdir command
a = 'mkdir nwdir'
b = os.popen(a,'r',1)
print (b)
Executing the above program outputs:
open file 'mkdir nwdir', mode 'r' at 0x81614d0