打开文件用open()函数

open(filename)默认为读取模式

等价于open(filename,'r')

1 txt=open(filename)

2 print txt.read()

3 txt.close() 

以上三行分别是将文件打开,将内容打印出来,将文件关闭。

 

 

 

文件写入用'w'

 1 line1="hello"
 2 line2="how are you "
 3 line3="bye"
 4 txt=open(filename,'w')
 5 txt.write(line1)
 6 txt.write(\n)
 7 txt.write(line2)
 8 txt.write(\n)
 9 txt.write(line3)
10 txt.close()

分别将三行写入文件后再关闭。

 

相关文章:

  • 2022-01-21
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2021-04-26
猜你喜欢
  • 2021-06-24
  • 2021-12-30
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案