文件 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。