在使用Python 3.7的同时使用read()和open()函数
是否有记录open()
但没有记录read()
的原因?
https://docs.python.org/3.7/library/functions.html
from sys import argv
script, filename = argv
txt = open(filename)
print(f"Here's your file {filename}:")
print(txt.read())
print("Type the filename again:")
file_again = input("> ")
txt_again = open(file_again)
print(txt_again.read())
答案 0 :(得分:5)
因为read
不是内置的function,而是method(*IO*
/ *Reader
中的file object。
您可以找到此类read
方法的文档:io.RawIOBase.read
,io.BufferedIOBase.read
,io.BufferedReader.read
,io.TextIOBase.read
,...
答案 1 :(得分:4)
因为read()
不是内置函数,因为它不以builtins.read
的形式出现。相反,它是_io.BufferedReader
类的方法。这意味着您必须创建该类的对象(通常使用内置函数open()
),然后为该对象调用read()
。
答案 2 :(得分:0)
因为read()
是file-object
的一种方法
答案 3 :(得分:0)
read
不是其他人所说的Python。您可以在许多I / O对象(例如文件对象,http请求等)中找到该方法。