Open() Function Of Os Module In Python


  • The implements the Unix function open(). This is a low-level function that accepts Unix file creation modes and file permissions as parameters to create a file descriptor from a given file path.

  • The Python built-in function open() dynamically creates and returns different types of objects based on the chosen I/O mode (text or raw), which is generally suitable for most of the common needs of file I/O.The file descriptor returned by can be used to perform I/O involving raw bytes or can be used to develop objects and functions to support specialised I/O (e.g., involving text).


path - File path

flags - Flags that are ORed together to specify read, write, create and other similar modes

mode - Octal literal specifying file permissions. The effective mode is given by (mode and (not mask value))


# Example Python program that uses low level function
# to create a file and perform I/O operations on it
import os

# Open a file on a given path
fpath  = "/PythonProgs/filetest.txt"
flgs   = os.O_RDWR | os.O_CREAT
fd     =, flgs, mode = 0o744)

# Write bytes to the file using the file descriptor
numBytes = os.write(fd, "hello world".encode())
print("Number of bytes written:%d"%numBytes)

# Read and print raw bytes
fd     =, os.O_RDWR, mode = 0o744)
bytesRead =, 8)

while bytesRead:
    bytesRead =, 8)



Number of bytes written:11
<class 'bytes'>
b'hello wo'


Copyright 2023 ©