×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Home » Python

Python File mode Property with Example

Python File mode Property: Here, we are going to learn about the mode Property, how to get the file mode from file object in Python?
Submitted by IncludeHelp, on December 24, 2019

File mode Property

mode Property is an inbuilt property of File object (IO object) in Python, it is used to get the mode (w, r, a, etc.) of the file from the file object in Python.

Syntax:

    file_object.mode

Parameter(s):

  • None

Return value:

The return type of this method is <class 'str'>, it returns the file mode.

Example:

# Python File mode Property with Example

# opening files in various modes
file1 = open("myfile1.txt", "w")
file2 = open("myfile3.txt", "a")
file3 = open("myfile4.txt", "wb")

# printing the file modes
print("file1 mode is:", file1.mode)
print("file2 mode is:", file2.mode)
print("file3 mode is:", file3.mode)

# closing the files
file1.close()
file2.close()
file3.close()

Output

file1 mode is: w
file2 mode is: a
file3 mode is: wb
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.