1 #文件的读取.py 2 a=open('test.txt').readline() #只读取文件第一行,保存为字符串格式 3 b=open('test.txt').read() #读取全部内容,保存为字符串格式 4 c=open('test.txt').readlines() #以每行为元素,生成一个列表(包括换行字符 ) 5 d=open('test.txt','r') #打开文件并用一个抽象变量:"文件句柄"来表示". 只读,同'rt'和不带'r'参数类似) 6 7 8 print(a) 9 print(b,' ') 10 print(c," ") 11 print(d,' ') 12 13 14 print(type(a)) 15 print(type(b)) 16 print(type(c)) 17 print(type(d))
结果如下:
1 300,0,144,1,0,0 2 3 300,0,144,1,0,0 4 300,0,144,0,1,0 5 300,0,144,1,0,1 6 300,0,144,0,0,1 7 300,0,144,1,1,0 8 9 ['300,0,144,1,0,0 ', '300,0,144,0,1,0 ', '300,0,144,1,0,1 ', '300,0,144,0,0,1 ', '300,0,144,1,1,0'] 10 11 <_io.TextIOWrapper name='test.txt' mode='r' encoding='cp936'> 12 13 14 <class 'str'> 15 <class 'str'> 16 <class 'list'> 17 <class '_io.TextIOWrapper'> 18 19 ***Repl Closed***