Python os.open() Method


Description

Python method open() opens the file file and set various flags according to flags and possibly its mode according to mode.The default mode is 0777 (octal), and the current umask value is first masked out.

Syntax

Following is the syntax for open() method −

os.open(file, flags[, mode]);

Parameters

Return Value

This method returns the file descriptor for the newly opened file.

Example

The following example shows the usage of open() 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")

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

This would create given file foo.txt and then would write given content in that file and would produce following result −

Closed the file successfully!!
python_files_io.htm