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.

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 you can use fsync() method.

# Infact here you would not be able to see its effect.

os.fsync(fd)

# 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 test

Closed the file successfully!!

os.ftruncate(fd, length)

Description

The method ftruncate() truncates the file corresponding to file descriptor fd, so that it is at most length bytes in

size.

Syntax

Following is the syntax for ftruncate() method:

os.ftruncate(fd, length)

Parameters

• fd -- This is the file descriptor, which needs to be truncated.

• length -- This is the length of the file where file needs to be truncated.

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python

import os, sys

TUTORIALS POINT

Simply Easy Learning

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

Saved successfully!

Ooh no, something went wrong!