Python 文件 IO


以下代码演示了Python基本的文件操作,包括 open,read,write:

  1. # Filename :test.py
  2. # author by : www.shouce.ren
  3.  
  4. # 写文件
  5. with open("test.txt", "wt") as out_file:
  6. out_file.write("该文本会写入到文件中\n看到我了吧!")
  7. # Read a file
  8. with open("test.txt", "rt") as in_file:
  9. text = in_file.read()
  10. print(text)

执行以上代码会在该输出结果为:

  1. 该文本会写入到文件中
  2. 看到我了吧!