我们可以打开文本文件,并通过read()读取文本文件:
f = open('file_txt.txt')
f.read()
print(f)
直接向文本文件中写入数据是不行的,因为文本文件的默认打开模式是只读模式'r',应该先指定可写的模式:
f1 = open('file_txt.txt','w')
f1.write('hello world!')