Change language

Python | os.open () method

os.open() in Python is used to open the specified file path and set various flags according to the specified flags and its mode in accordance with the specified mode. 
This method returns the file descriptor for the newly opened file. The returned file descriptor is not inherited.

Syntax: os.open (path, flags, mode = 0o777, *, dir_fd = None)

Parameters:
Path: A path-like object representing the file system path. This is the file path to be opened.
A path-like object is a string or bytes object which represents a path.
flags: This parameter specify the flags to be set for newly opened file.
mode (optional): A numeric value representing the mode of the newly opened file. The default value of this parameter is 0o777 (octal).
dir_fd (optional): A file descriptor referring to a directory.

Return Type: This method returns a file descriptor for newly opened file.

Code: using os.open () to open the file path

# Python program to explain the os. open ()

 
# import of the os module

import os

 

 
# Path to file to open

path = ’. / file9.txt’ < / code>

 
# The mode must be set

mode = 0o666

 
# flags

flags = os.O_RDWR | os.O_CREAT

 

 
# Open the specified file path
# using the os.open () method
# and get the file descriptor for
# path to the open file

fd = os. open (path, flags, mode)

 

print ( "File path opened successfully." )

 

 
# Write line to file
# using file descriptor

str = "GeeksforGeeks: A computer science portal for geeks."

os.write (fd, str . encode ())

print ( "String written to the file descriptor. "

  

  
# Now read the file
# from the beginning

os.lseek (fd, 0 , 0 )

str = os.read (fd, os.path.getsize (fd))

print ( "String read from the file descriptor:" )

print ( str . decode ())

 
# Close the file descriptor
os.close (fd )

print ( "File descriptor closed successfully. " )

Output:

 File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully. 

Link: os.open rel=noopener target=_blank> https://docs.python.org/3/library/os.html#os.open

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically