28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Description

The method popen() opens a pipe to or from command.The return value is an open file object connected to the

pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.The bufsize argument has the

same meaning as in open() function.

Syntax

Following is the syntax for popen() method:

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

Parameters

• command -- This is command used.

• mode -- This is the Mode can be 'r'(default) or 'w'.

• bufsize -- If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering

will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then

buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system

default(default behavior).

Return Value

This method returns an open file object connected to the pipe.

Example

The following example shows the usage of popen() method.

# !/usr/bin/python

import os, sys

# using command mkdir

a = 'mkdir nwdir'

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

print b

Let us compile and run the above program, this will produce the following result:

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

os.read(fd, n)

Description

The method read() read at most n bytes from file desciptor fd, return a string containing the bytes read. If the end

of file referred to by fd has been reached, an empty string is returned.

Syntax

Following is the syntax for read() method:

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!