Created
January 31, 2019 12:33
-
-
Save 01x01/5f3a591c6da78ae2afd0bb884d13243f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 读取文件 | |
# 调用read()会一次性读取文件的全部内容,如果文件有10G,内存就爆了, | |
# 可以反复调用read(size)方法,每次最多读取size个字节的内容。 | |
# 调用readline()可以每次读取一行内容, | |
# 调用readlines()一次读取所有内容并按行返回list。因此,要根据需要决定怎么调用 | |
with open(filepath,'r')as f | |
f.read() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 写入文件 | |
# write 写入文件内容 | |
# 接收一个列表,写入文件 | |
with open("test.txt",'w',encoding="utf-8")as f: | |
f.writelines(["sfsdfsd",'sdfsdfsdf']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment