Python os.popen() Method
os.popen() Overview
The os.popen() method is used to open a pipe from a command.
Valid in Unix, Windows
os.popen() Syntax
The syntax format of the popen() method is as follows.
os.popen(command[, mode[, bufsize]])
os.popen() Parameters
-
command – The command to use.
-
mode – Mode permissions 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 use of parameter size buffers (approximate values, in bytes)
-
A negative bufsize means using the system default value
In general, it is line buffered for tty devices; for other files, it is fully buffered. If no parameter is changed, the system default is used.
-
os.popen() Return Value
Returns an open file object with the file descriptor fd
os.popen() Example
The following example demonstrates the use of the popen() method.
#!/usr/bin/python3
import os, sys
# Use the mkdir command
a = 'mkdir nwdir'
b = os.popen(a,'r',1)
print (b)
Output: