3-Python文件操作总结

文件操作

文件读取

>>> f = open('/Users/michael/test.txt', 'r')

r表示读。

>>> f.read()
'Hello, world!'
>>> f.close()

引入with语句来自动帮我们调用close()方法:

with open('/path/to/file', 'r') as f:
    print(f.read())

文件写入

你可能感兴趣的:(3-Python文件操作总结)