读文件
def writeTextFile():
try:
content = 'Hello,xk,啊啊啊'
content.encode('UTF-8')
f = open('E:\\test.txt','w') # w 代表读
f.write(content)
except BaseException as e:
print(e)
finally:
if f:
f.close()
# 调用:
if __name__ == '__main__':
writeTextFile()
读文件:
def readFile():
try:
f = open('E:\\test.txt','r') # r 代表读
# print(f.read()) # read()会⼀次性读取⽂件的全部内容
print(f.readlines()) # 每次读取⼀⾏内容,返回list
except BaseException as e:
print(e)
finally:
if f :
f.close()
# 调用:
if __name__ == '__main__':
readFile()
网友评论