zoukankan      html  css  js  c++  java
  • python: 文件的读写

     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***
    蜗牛跑的慢,是因为没有腿儿!
  • 相关阅读:
    第二阶段第二次spring会议
    第一次冲刺阶段的改进方案
    第二阶段第一次spring会议
    第七次spring会议
    第六次spring会议
    第五次spring会议
    第四次spring会议
    第三次spring会议
    第二次spring会议
    第一次spring会议
  • 原文地址:https://www.cnblogs.com/xier/p/10441238.html
Copyright © 2011-2022 走看看