Python program to check a file's status in file Handling

Here, we will see a Python program to check the file's status in file Handling using in-build methods on file objects.
By Shivang Yadav Last updated : July 05, 2023

File Handling in Python is reading and writing data from files. Programs can interact with files and data using the concept of file handling.

The file object contains some variables and methods, we will be using name, closed, mode variable to see all status of file objects.

Python program to check a file's status in file handling

# Program to check a file's status in file Handling...

# main method 
def main():
    # Opening the file 
    fileObject = open("file.dat","rb")

    # Printing object's status
    print("Name of the File : ",fileObject.name)
    print("Closed or Not    : ",fileObject.closed)
    print("Opening Mode     : ",fileObject.mode)

    fileObject.close()

    print("")
    print("Closed or Not    : ",fileObject.closed)

if __name__=="__main__":main()

File : File.dat

001,John,35000,London
002,Jane,50000,NewYork
003,Ram,125000,Delhi

Output:

Name of the File :  file.dat
Closed or Not    :  False
Opening Mode     :  rb

Closed or Not    :  True

Explanation:

In the above code, we have opened a file using a file object named fileObject. Using this we have checked for the object's name, status, and mode.

Python file handling programs »





Comments and Discussions!

Load comments ↻






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