笨方法学python--读写文件

  1 文件相关的函数

close

read

readline  读取文本文件中的一行

truncate  清空文件

write('adb') 写入

  2 写文件,首先要在open时,写入权限w

target = open(filename, 'w')

当使用w权限时,即使不使用target.truncate(),后面write后也是代替原内容,相当于先truncate了

若是使用a权限,可以在文件末尾追加新write内容。

target = open(filename, 'a')

若不写权限,相当于默认为r,只读

另外,有r+, w+,a+, 在下篇文件中讲述它们的不同

  3  write可以串写参数,简化代码,如下面

target.write(line1)

target.write(" ")

target.write(line2)

target.write(" ")

target.write(line3)

target.write(" ")

可以写成 target.write(line1+" "+line2+" "+line3+" ")

 
原文地址:https://www.cnblogs.com/guohuino2/p/5978345.html