Q:

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

belongs to collection: Python file handling programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to read data from file and extract ... >>
<< Python program to read first N character from each...