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

Python__基本文件操作

时间:2017-07-12 23:04:41      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:pass   with   读取   enc   class   打开   pytho   系统   write   

##open将操作系统发起请求,操作系统完成请求,
# f = open(‘old.txt‘,‘r‘,encoding = ‘utf-8‘)
# res = f.read()
# print(res)
# f.close()

# f = open(‘old.txt‘,‘r‘,encoding = ‘utf-8‘)
# res = f.readline()##读取一行
# print(res)
# f.close()


##读完文件之后必须要关闭,否则文件将一直保存在操作系统中

##执行以下操作可以在打开文件之后,自动将文件关闭
# with open(‘old.txt‘,‘r‘,encoding = ‘utf-8‘) as f:
#     res = f.read()
#     print(res)
#     pass

# f = open(‘old.txt‘,‘w‘,encoding = ‘utf-8‘)
# f.write(‘111\n‘)
# f.write(‘222\n‘)
# f.writelines([‘a\n‘,‘b\n‘,‘c\n‘])##表示的是可以写入多行
# f.close()

 

Python__基本文件操作

标签:pass   with   读取   enc   class   打开   pytho   系统   write   

原文地址:http://www.cnblogs.com/wangmengzhu/p/7157617.html

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