Python基本文件操作

##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()

posted @ 2017-11-03 15:29  黄骁瀚cerny  阅读(124)  评论(0编辑  收藏  举报