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.

# Open a file

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

# Write one string

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

# Now you can use ftruncate() method.

os.ftruncate(fd, 10)

# Now read this file from the beginning.

os.lseek(fd, 0, 0)

str = os.read(fd, 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 te

Closed the file successfully!!

os.getcwd()

Description

The method getcwd() returns current working directory of a process.

Syntax

Following is the syntax for getcwd() method:

os.chdir(path)

Parameters

• NA

Return Value

This method returns current working directory of a process.

Example

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

#!/usr/bin/python

import os, sys

# First go to the "/var/www/html" directory

os.chdir("/var/www/html" )

# Print current working directory

print "Current working dir : %s" % os.getcwd()

TUTORIALS POINT

Simply Easy Learning

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

Saved successfully!

Ooh no, something went wrong!