python 打印文件名_在Python中打印文件名,关闭状态和文件模式

本文介绍了如何在Python中使用file_object.name获取文件名,使用file_object.closed检查文件是否关闭,以及使用file_object.mode查看文件的打开模式。通过示例展示了这些属性的用法。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值