文件 fileno() 方法
fileno() 方法是 Python 中的内置方法,用于获取文件编号,即文件描述符作为流的整数。如果操作系统不使用文件描述符关闭文件,它可能会返回错误。
用法:
file_object.fileno()
参数:
- 它不接受任何参数。
返回值:
这个方法的返回类型是<class 'int'>
,它返回一个整数值,它是文件的文件描述符。
范例1:
# Python File fileno() Method with Example
# creating two files
myfile1 = open("hello1.txt", "w")
myfile2 = open("hello2.txt", "w")
# printing the file descriptors
print("files are in write mode...")
print("myfile1.fileno():", myfile1.fileno())
print("myfile2.fileno():", myfile2.fileno())
# closing the files
myfile1.close()
myfile2.close()
# opening file in Read modes
myfile1 = open("hello1.txt", "r")
myfile2 = open("hello2.txt", "r")
# printing the file descriptors
print("files are in read mode...")
print("myfile1.fileno():", myfile1.fileno())
print("myfile2.fileno():", myfile2.fileno())
# closing the files
myfile1.close()
myfile2.close()
输出
files are in write mode... myfile1.fileno(): 5 myfile2.fileno(): 6 files are in read mode... myfile1.fileno(): 5 myfile2.fileno(): 6
范例2:
# Python File fileno() Method with Example
# creating a file
myfile1 = open("hello1.txt", "w")
# printing the file descriptor
print("myfile1.fileno():", myfile1.fileno())
# closing the files
myfile1.close()
# trying to print the file descriptor
# after closing the file
# error will be returned
# printing the file descriptor
print("myfile1.fileno():", myfile1.fileno())
输出
myfile1.fileno(): 5 Traceback (most recent call last): File "main.py", line 16, in <module> print("myfile1.fileno():", myfile1.fileno()) ValueError:I/O operation on closed file
相关用法
- Python File flush()用法及代码示例
- Python File tell()用法及代码示例
- Python File seek()用法及代码示例
- Python File write()用法及代码示例
- Python File read()用法及代码示例
- Python File truncate()用法及代码示例
- Python File open()用法及代码示例
- Python File isatty()用法及代码示例
- Python File writable()用法及代码示例
- Python File writelines()用法及代码示例
- Python File readable()用法及代码示例
- Python File readlines()用法及代码示例
- Python File close()用法及代码示例
- Python File readline()用法及代码示例
- Python File seekable()用法及代码示例
- Python numpy.less()用法及代码示例
- Python Sympy Permutation.list()用法及代码示例
- Python Matplotlib.figure.Figure.subplots_adjust()用法及代码示例
- Python numpy.tril()用法及代码示例
- Python Matplotlib.pyplot.matshow()用法及代码示例
注:本文由纯净天空筛选整理自 Python File fileno() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。