YouTip LogoYouTip

Python3 Os Open

# Python3.x Python3 os.open() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.open() method is used to open a file and set the required opening options. The mode parameter is optional and defaults to 0777. ### Syntax The syntax for the **open()** method is as follows: os.open(file, flags[, mode]); ### Parameters * **file** -- The file to be opened. * **flags** -- This parameter can be one of the following options, separated by "|" if multiple are used: * **os.O_RDONLY:** Open in read-only mode. * **os.O_WRONLY:** Open in write-only mode. * **os.O_RDWR:** Open in read-write mode. * **os.O_NONBLOCK:** Open without blocking. * **os.O_APPEND:** Open in append mode. * **os.O_CREAT:** Create and open a new file. * **os.O_TRUNC:** Open a file and truncate its length to zero (requires write permission). * **os.O_EXCL:** Return an error if the specified file exists. * **os.O_SHLOCK:** Automatically acquire a shared lock. * **os.O_EXLOCK:** Automatically acquire an exclusive lock. * **os.O_DIRECT:** Eliminate or reduce caching effects. * **os.O_FSYNC:** Synchronous write. * **os.O_NOFOLLOW:** Do not follow symbolic links. * **mode** -- Similar to [chmod()](#). ### Return Value Returns the file descriptor of the newly opened file. ### Example The following example demonstrates the use of the open() method: #!/usr/bin/python3 import os, sys # Open file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write string os.write(fd, str.encode("This is test"))# Close file os.close( fd )print ("File closed successfully!!") The output of executing the above program is: File closed successfully!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Heap Shift UpSorting Algorithm Derivative P β†’