The 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.
file -- File name to be opened.
flags -- The following constants are options for the flags. They can be combined using the bitwise OR operator |. Some of them are not available on all platforms.
mode -- This work in similar way as it works for chmod() method.
os.open(file, flags[, mode]);
#!/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!!