python file modes

Do you want to know the information about “python file modes”. If yes, you’re in the correct tutorial.

python file modes

r for reading
r+ opens for reading and writing (cannot truncate a file)
w for writing
w+ for writing and reading (can truncate a file)
rb for reading a binary file. The file pointer is placed at the beginning of the file.
rb+ reading or writing a binary file
wb+ writing a binary file
a+ opens for appending
ab+ Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode.
x open for exclusive creation, failing if the file already exists (Python 3)
Mode:   Description:
"r"		# Opens a file for reading. (default)
"w"		# Opens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
"x"		# Opens a file for exclusive creation. If the file already exists, the operation fails.
"a"		# Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.
"t"		# Opens in text mode. (default)
"b"		# Opens in binary mode.
"+"		# Opens a file for updating (reading and writing)
with open('pagehead.section.htm','r') as f:
    output =
>>> with open('workfile') as f:
...     read_data =

>>> # We can check that the file has been automatically closed.
>>> f.closed

Final Words

I hope this article helps you to know about “python file modes”. If you have any queries regarding this post please let us know via the comment section. Share this post with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment