poem ='''There was a young lady named Bright, Whose speed was far faster than light; She started one day In a relative way, And returned on the previous night.''' print('len(poem)',len(poem)) fout =open('relativity','wt') print(fout.write(poem)) fout.close()
try:fout =open('relativity','xt')fout.write('stomp stomp stomp') except FileExistsError:print('relativity already exists! That was a close one.')
read()
不带参数的read() 一次读取文件的所有内容
fin =open('relativity','rt') poem = fin.read() fin.close() print(len(poem))
设置最大读入限制
读到文件结尾,再调用 read() 函数会返回空字符串’’ ,
poem ='' fin =open('relativity','rt') chunk =100 whileTrue:fragment = fin.read(chunk)ifnot fragment:breakpoem += fragment fin.close() print(len(poem))
poem ='' fin =open('relativity','rt') for line in fin:poem += linefin.close() print(len(poem))
readlines()
读入所有的行,并返回单行字符串的列表
print() 默认在每行结束加上换行
fin =open('relativity','rt') lines = fin.readlines() fin.close() print(len(lines)," lines read") for line in lines:print(line, end='')print('newline')for line in lines:print(line)
Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ...
[详细]