Python 3 - os.close() Method


Description

The method close() closes the associated with file descriptor fd.

Syntax

Following is the syntax for close() method −

os.close(fd)

Parameters

fd − This is the file descriptor of the file.

Return Value

This method does not return any value.

Note − This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe().

Example

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

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "this is test" 

# string needs to be converted byte object
b = str.encode(line)
os.write(fd, b)

# Close opened file
os.close( fd )

print ("Closed the file successfully!!")

Result

When we run the above program, it produces the following result −

Closed the file successfully!!
python_files_io.htm
Advertisements