Python os.open() Method

Musfirah Waseem Jan 30, 2023 Aug 04, 2022
  1. Syntax of the os.open() Method
  2. Example Codes: Working With the os.open() Method in Python
  3. Example Codes: Read the File After Using the os.open() Method in Python
  4. Example Codes: Use the os.fdopen Method With the os.open() Method in Python
Python os.open() Method

Python os.open() method is an efficient way of creating OS-level file descriptors and setting various flags and modes accordingly.

Syntax of the os.open() Method

os.open(path address, flag, mode)
os.open(path address, flag, mode, fd_dir)

Parameters

path address It is an address object of a file system path or a symlink. The object can either be str or bytes.
flag List of all the flags that could be set on the new file:
1. os.O_RDONLY: flag used to open a file for reading only
2. os.O_WRONLY: flag used to open a file for writing only
3. os.O_RDWR: flag used to open a file for reading and writing both
4. os.O_NONBLOCK: flag used not to block an opened file
5. os.O_APPEND: flag used to append a file on each write
6. os.O_CREAT: flag used to create a file if it already doesn’t exist
7. os.O_TRUNC: flag used to truncate a file size to 0
8. os.O_EXCL: flag used to show an error while creating a file that already exists
9. os.O_SHLOCK: flag used to obtain a shared lock for a file atomically
10. os.O_EXLOCK: flag used to obtain an exclusive lock for a file atomically
11. os.O_DIRECT: flag used to remove or reduce cache effects in a file
12. os.O_FSYNC: flag used to write file synchronously
13. os.O_NOFOLLOW: flag used to set the file that doesn’t follow a symlink
mode It is an optional parameter representing the mode of the newly opened file. The default value of the mode is an octal numeric integer 0o777.
fd_dir It is an optional parameter representing a file descriptor referring to a path.

Return

This method returns the newly opened file’s path and file descriptor.

Example Codes: Working With the os.open() Method in Python

import os

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

os.write(fd, 'Hello, World!'.encode())

os.close( fd )

print ("The file has been closed successfully!")

Output:

The file has been closed successfully!

While using the os.open() method, we can use one or more modes simultaneously by using the vertical bar |.

Example Codes: Read the File After Using the os.open() Method in Python

import os

path_address = './File.txt'

mode_set = 0o666

flags = os.O_CREAT | os.O_RDWR

descriptor = os.open(path_address, flags, mode_set)

print("The new file has been created.")

line = "Hello, World! Programming is fun!"

os.write(descriptor, line.encode())

print("Text has been successfully entered in the file.")

os.lseek(descriptor, 0, 0)

line = os.read(descriptor, os.path.getsize(descriptor))

print("Reading the text from the file:")

print(line.decode())

os.close(descriptor)

print("The file descriptor has been closed successfully.")

Output:

The new file has been created.
Text has been successfully entered in the file.
Reading the text from the file:
Hello, World! Programming is fun!
The file descriptor has been closed successfully.

It is a healthy practice for any programmer to close all the newly opened files using the os.close() method.

Example Codes: Use the os.fdopen Method With the os.open() Method in Python

import os

with os.fdopen(os.open("File.txt",os.O_CREAT | os.O_RDWR ),'w') as fd:
    fd.write("Hello, World!")

print ("The file descriptor has been closed successfully.")

Output:

The file descriptor has been closed successfully.

Note that the returned file descriptor from this method is non-inheritable.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS