Python 文件 read() 方法
定义和用法
read() 方法从文件返回指定的字节数。默认值为 -1,表示整个文件。
实例
例子 1
读取文件 "demofile.txt" 的内容:
f = open("demofile.txt", "r")
print(f.read())
例子 2
读取文件 "demofile.txt" 的内容:
f = open("demofile.txt", "r")
print(f.read(35))
语法
file.read()
参数
| 参数 | 描述 |
|---|---|
| size | 可选。要返回的字节数。默认值 -1,表示整个文件。 |