Python method open() opens the file and sets 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:
os.open(file, flags[, mode]);
This method returns the file descriptor for the newly opened file.
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 the given file foo.txt and then would write given content in that file and would produce the following result:
Closed the file successfully!!
Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.