python 打印文件名
Prerequisite: Opening, closing a file/open(), close() functions in Python
先决条件: 在Python中打开,关闭文件/ open(),close()函数
1)文件名(file_object.name) (1) File name (file_object.name))
To get the file name, we use file_object.name – it returns the opened file name with extension.
要获取文件名,我们使用file_object.name-它返回带有扩展名的打开的文件名。
2)文件关闭状态(file_object.closed) (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".
为了找到文件关闭状态,即检查文件是打开还是关闭,我们使用file_object.close 。 如果打开文件,则返回“ True” ,否则返回“ False” 。
3)文件模式(file_object.mode) (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).
为了找到打开文件的当前文件打开模式,我们使用file_object.mode 。 它返回文件打开模式,如“ wt” , “ rt”等,我们在上一篇文章中已经讨论过( 在Python中打开,关闭文件/ open(),close()函数 )。
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
翻译自: https://www.includehelp.com/python/printing-file-name-closed-status-and-file-mode.aspx
python 打印文件名