当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python File writable()用法及代码示例


文件 writable() 方法

writable() 方法是 Python 的内置方法,用于检查文件流在 Python 中是否可写?

用法:

    file_object.writable()

参数:

  • 它不接受任何参数。

返回值:

这个方法的返回类型是<class 'bool'>, 如果文件流可写则返回 True ,如果文件不可写则返回 False 。

例:

# Python File writable() Method with Example

# creating a file
myfile1 = open("hello1.txt", "w")

# checking whethet the file stream is 
# writable or not
print("myfile1.writable():", myfile1.writable())

# closing the file
myfile1.close()

# opening file in read mode
myfile1 = open("hello1.txt", "r")

# checking whethet the file stream is 
# writable or not
print("myfile1.writable():", myfile1.writable())

# closing the file
myfile1.close()

输出

myfile1.writable():True
myfile1.writable():False


相关用法


注:本文由纯净天空筛选整理自 Python File writable() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。