IO 文件读写
读:
f = open('/Users/panbingqing/Desktop/test/a.rtf') #读模式,不可做任何修改
#将整个文件内容读取为一个字符串值,使用read()方法
f1 = f.read()
#or use readlines()将整个文件读取为列表
f2 = f.readlines()
type(f2) #str
type(f1) #list
写:IO 写文件,'w’以覆写模式打开,'a’以添加模式打开,两者都会创建一个新的空文件,最后要调用close()方法
f3 = open('/Users/panbingqing/Desktop/test/f3.txt','w')
f3.write('Hello world!\n')
f3.close()
f4 = open('/Users/panbingqing/Desktop/test/f3.txt','a')
f4.write('bacon is nor a vegetable')
f4.close()
f5 = open('/Users/panbingqing/Desktop/test/f3.txt')
content = f5.read()
f5.close()
print(content)```