Python File mode Property with Examples

Files in Python:

Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

Files are identified locations on a disc where associated data is stored. They are used to retain data in non-volatile memory permanently (e.g. hard disk).

Because Random Access Memory (RAM) is volatile (it loses data when the computer is shut off), we employ files to store data for future use by permanently storing it.

When we wish to read or write to a file, we must first open it. When we’re finished, it needs to be closed so that the resources associated with the file may be released.

As a result, in Python, a file operation occurs in the following order:

  • Open a file
  • Reading or writing (performing operation)
  • close the file.
              Mode                         Description
     r  Allows you to read a file. (default)
    w Allows you to write to a file. If the file does not exist, it is created; otherwise, it is truncated.
    x Allows you to open a file for exclusive creation. The operation fails if the file already exists.
    a Opens a file for adding/appending at the end without truncating it. If the file does not exist, it is created.
    t opens in text mode. (default)
    b Opens in binary mode.
    + Opens a file for modification or updating (reading and writing)

File mode Property in Python:

mode Property is a built-in property of the File object (IO object) in Python that is used to retrieve the file’s mode (write(w), read(r), append(a), etc.) from the file object.

Syntax:

fileobject.mode

Parameters: This method doesn’t accept any parameters.

Return Value: 

This method’s return type is <class’str’>, and it returns the file mode.

File mode Property with Examples in Python

Example

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in write mode. In this case, we’re writing the contents into the file.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Open the file in write-binary mode. In this case, we’re simply writing the contents in binary mode into the file.
  • Print the mode of the above file samplefile_1 using the mode property.
  • Print the mode of the above file samplefile_2 using the mode property.
  • Print the mode of the above file samplefile_3 using the mode property.
  • Close the above file samplefile_1 using the close() function.
  • Close the above file samplefile_2 using the close() function.
  • Close the above file samplefile_3 using the close() function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in write mode. In this case, we're writing the contents into the file.
samplefile_1 = open("givenFilename", "w")
# Open the file in read mode. In this case, we're simply reading the contents of the file.
samplefile_2 = open("givenFilename", "r")
# Open the file in write-binary mode. In this case, we're simply writing the contents in binary mode into the file.
samplefile_3 = open("givenFilename", "wb")

# Print the mode of above file samplefile_1 using the mode property
print("The mode of samplefile_1:", samplefile_1.mode)
# Print the mode of above file samplefile_2 using the mode property
print("The mode of samplefile_2:", samplefile_2.mode)
# Print the mode of above file samplefile_3 using the mode property
print("The mode of samplefile_3:", samplefile_3.mode)

# Close the above file samplefile_1 using the close() function
samplefile_1.close()
# Close the above file samplefile_2 using the close() function
samplefile_2.close()
# Close the above file samplefile_3 using the close() function
samplefile_3.close()

Output:

The mode of samplefile_1: w 
The mode of samplefile_2: r 
The mode of samplefile_3: wb

Google Colab Images:

Files and Code: