Python Method


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.


Following is the syntax for open() method −, flags[, mode]);


Return Value

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


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


import os, sys

# Open a file
fd = "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!!