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).

Example:

# 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
fo.close()

print("")

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

Output

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

Closed or Not    :  True





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



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.