python对txt的读写

filename ="D://练习//learning_python.txt"
#读取整个文件
with open (filename) as file_obj:
    contents = file_obj.read()
    print(contents)
#逐行读取
with open (filename) as file_obj:
    lines = file_obj.readlines()
    print(lines)
    b=lines
#把读出来的添加到列表
a =[]
for b in lines:
    print(b)
    a.append(b)
print(a)

#写文件
with open (filename,"w") as obj:
    obj.write("china wan sui!")
    obj.write("china people wan sui!")
    obj.write("china dang wan sui!")
#写完了读整个文件
with open (filename)as obj:    
    i = obj.read()
    for a in i:
        print(a)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2022-01-25
  • 2022-12-23
  • 2021-11-01
  • 2021-07-29
  • 2022-12-23
相关资源
相似解决方案