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 打印文件名