#!/usr/bin/env python
# -*- coding: utf-8 -*-
#打开文件
try:
f = open("/Users/malinkang/Documents/Git/PythonNote/oop.md",'r')
print f.read()
finally:
if f:
f.close()
#Python引入了with语句来自动帮我们调用close()方法
with open("/Users/malinkang/Documents/Git/PythonNote/oop.md", 'r') as f:
print f.read()
#每次读取一行
with open("/Users/malinkang/Documents/Git/PythonNote/SUMMARY.md", 'r') as f:
for line in f.readlines():
print(line.strip())# 把末尾的'\n'删掉
# 写入文件
with open("/Users/malinkang/Documents/Git/PythonNote/SUMMARY.md", 'w') as f:
print f.write('Hello,world')