ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Shivang Yadav, on March 16, 2021

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.

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

# 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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.