增加对Python read()和open()函数的理解

use*_*264 -2 python python-3.x

使用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())
Run Code Online (Sandbox Code Playgroud)

fal*_*tru 5

因为read不是内置的功能,但一个方法文件对象(*IO*/ *Reader).

你可以找到这样的documetations read方法:io.RawIOBase.read,io.BufferedIOBase.read,io.BufferedReader.read,io.TextIOBase.read,...