1 f = open('lyrics') #打开文件
2 first_line = f.readline()#一行一行读
3 print('---'.center(50,'-'))
4 data = f.read()# 读取剩下的所有内容,文件大时不要用
5 print(data) #打印文件
6 f.close() #关闭文件
打开文件有:读、写、添加。
r,只读模式(默认)。
w,只写模式。【不可读;不存在则创建;存在则删除内容;】
a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
1 f = open('listen','r',encoding='utf-8')
2 f = open('listen','w',encoding='utf-8')
3 f = open('listen','a',encoding='utf-8')
这叫做句柄语句
若要把第九行去掉,可用下面方法:

1 for index,line in enumerate(f.readlines()) : #可以取出行号
2 if index == 9 :
3 print('abc'.center(50,"#"))
4 continue
5 print(line.strip())
用readlines()显示是一个列表

1 f = open('listen','r',encoding='utf-8')
2
3 print(f.readlines()) #为一个列表
4
5 ['Somehow, it seems the love I knew was always the most destructive kind\n', '不知为’]
来源:https://www.cnblogs.com/handsometiger28/p/12296275.html
