Python 基礎教程
read() 方法用于從文件讀取指定的字節(jié)數(shù),如果未給定或為負則讀取所有。
read() 方法語法如下:
fileObject.read();
size -- 從文件中讀取的字節(jié)數(shù)。
返回從字符串中讀取的字節(jié)。
以下實例演示了 read() 方法的使用:
文件 json.txt 的內(nèi)容如下:
1: 2: 3: 4: 5:
循環(huán)讀取文件的內(nèi)容:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打開文件 fo = open("json.txt", "rw+") print "文件名為: ", fo.name line = fo.read(10) print "讀取的字符串: %s" % (line) # 關(guān)閉文件 fo.close()
以上實例輸出結(jié)果為:
文件名為: json.txt 讀取的字符串: 1:www.runo其他擴展