码迷,mamicode.com
首页 > 编程语言 > 详细

python 文件读写

时间:2018-09-28 15:21:42      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:write   close   读取文件   txt   str   一个   file   des   content   

读写文件3个步骤

  • 调用open()函数,返回一个File对象
  • 调用File对象的read()或write()方法
  • 调用File对象的close()方法,关闭该文件

读取文件:

  • helloFile = open(‘C:\\Users\\Administrator\\Desktop\\url.txt‘)  
  • helloContent = helloFile.read()
  • helloFile.close()

写入文件

  • helloFile = open(‘C:\\Users\\Administrator\\Desktop\\url.txt‘, ‘w‘)  
  • helloFile.write(‘Hello world!\n‘)
  • helloFile.close()

python 文件读写

标签:write   close   读取文件   txt   str   一个   file   des   content   

原文地址:https://www.cnblogs.com/luckyls/p/9718607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!