Python读写文件

来源:互联网 发布:淘宝店铺动态 编辑:程序博客网 时间:2024/04/28 22:34

读写文件常用方法

close-关闭文件。相当于保存文件

read-读取文件内容。可以把文件内容赋值给一个变量,通过这个方法可以获得文件的全部内容。

readline-获取文件中的一行

truncate-清空文件,请小心使用

write(stuff)-将stuff写入文件

filename = input("filename >")txt = open(filename,'r')# line1 = txt.readline()print("Opening the file")for line in txt:    print(line)    for list in line:        print(list)
txt.write("hello,hello,hello,hello,hello,hello,hello,hello,hello,hello,hello,hello")


0 0
原创粉丝点击