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("\n")

target.write(line2)

target.write(line3)

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

 

相关文章:

  • 2021-07-20
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-12-29
  • 2022-01-14
  • 2022-01-22
猜你喜欢
  • 2022-01-12
  • 2021-04-12
  • 2021-05-08
  • 2022-12-23
  • 2021-12-19
  • 2022-02-22
  • 2022-12-23
相关资源
相似解决方案