28.10.2021 Views

Python Tutorial ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example

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

#!/usr/bin/python

import os, sys

# Open a file

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string

os.write(fd, "This is test")

# Now duplicate this file descriptor as 1000

fd2 = 1000

os.dup2(fd, fd2);

# Now read this file from the beginning using fd2.

os.lseek(fd2, 0, 0)

str = os.read(fd2, 100)

print "Read String is : ", str

# Close opened file

os.close( fd )

print "Closed the file successfully!!"

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

Read String is : This is test

Closed the file successfully!!

os.fchdir(fd)

Description

The method fchdir() change the current working directory to the directory represented by the file descriptor fd.

The descriptor must refer to an opened directory, not an open file.

Syntax

Following is the syntax for fchdir() method:

os.fchdir(fd);

Parameters

• fd -- This is Directory descriptor.

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python

TUTORIALS POINT

Simply Easy Learning

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

Saved successfully!

Ooh no, something went wrong!