文件操作时,打开后操作完应该及时关闭 采用以下方面打开 可以自动关闭
1 #打开文件filsname,变量名data 2 with open(filsname,'r',encoding='utf-8') as data: 3 for line in data: 4 print(line) 5 6 同时打开两个文件示例 7 with open(filsname,'r',encoding='utf-8') as data, 8 open(filsname2,'r',encoding='utf-8') as data2: 9 10 for line in data2: 11 print(line)