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):
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