在Python中打印文件名,关闭状态和文件模式
先决条件:打开,关闭文件/open(),close()在Python功能
1)文件名(file_object.name)
要获取文件名,我们使用file_object.name-它返回带有扩展名的打开的文件名。
2)文件关闭状态(file_object.closed)
为了找到文件关闭状态,即检查文件是打开还是关闭,我们使用file_object.close。如果打开文件,则返回“True”,否则返回“False”。
3)文件模式(file_object.mode)
为了找到打开文件的当前文件打开模式,我们使用file_object.mode。它返回文件打开模式像“WT”,“RT”等,这是我们在过去后已经讨论(打开,关闭文件/open(),close()在Python函数)。
示例
#以写模式打开文件
fo = open("data.txt","wt")
#打印文件名
print("Name of the File : ",fo.name)
#定价文件关闭状态
print("Closed or Not : ",fo.closed)
#打印文件打开方式
print("Opening Mode : ",fo.mode)
#关闭档案
fo.close()
print("")
#定价文件关闭状态 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