Home » Python

Printing file name, closed status and file mode in Python

In this tutorial, we are going to learn about how to find, print file name, file close status, file opening mode in python?
Submitted by IncludeHelp, on December 27, 2018

Prerequisite: Opening, closing a file/open(), close() functions in Python

1) File name (file_object.name)

To get the file name, we use file_object.name – it returns the opened file name with extension.

2) File Close status (file_object.closed)

To find the file close status i.e. to check whether a file is opened or closed, we use file_object.close. It returns "True", if the file is opened otherwise it returns "False".

3) File Mode (file_object.mode)

To find the current file opening mode in which the file is opened, we use file_object.mode. It returns file opening mode like "wt", "rt" etc, which we have discussed in the previous post (Opening, closing a file/open(), close() functions in Python).


# opening a file in write mode
fo = open("data.txt","wt")

# printing name of the file 
print("Name of the File : ",fo.name)
# priting file closed status
print("Closed or Not    : ",fo.closed)
# printing file Opening mode
print("Opening Mode     : ",fo.mode)

# closing the file


# priting file closed status after closing the file
print("Closed or Not    : ",fo.closed)


Name of the File :  data.txt
Closed or Not    :  False
Opening Mode     :  wt

Closed or Not    :  True

Comments and Discussions!

Load comments ↻

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