python对txt的读写


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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM