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)
原文地址:https://www.cnblogs.com/hainabaichuan/p/11830769.html
时间: 2024-11-06 13:52:36