Python read读取文件教程
在
read函数详解
语法
s = fileObject.read(size)
参数
参数
描述
s
读取到的文件内容。
fileObject
文件对象。
size
可选参数,需要读取的字节数。
说明
read 函数接收一个 size
read函数读取文件特点
使用 read
案例
使用read函数读取文件
使用 read 函数读取文件内容
print("嗨客网(www.haicoder.net)")
file = open("C:/haicoder.txt")
s = file.read()
print("File Content =", s)
file.close()
程序运行后,控制台输出如下:
我们使用 open 函数以只读模式打开文件,接着使用 open 函数返回的文件对象调用 read 函数读取文件,并返回文件内容 s。文件读取成功后,需要使用
使用read函数指定读取文件的字节数
使用 read 函数的 size 参数,指定读取文件的字节数
print("嗨客网(www.haicoder.net)")
file = open("C:/haicoder.txt")
s =