文件IO

Python笔记:文件IO操作

读取文件 使用open()打开文件,文件不存在会抛出IOError错误。 try: f = open(‘/path/to/file’, ‘r’) print(f.read()) finally: if f: f.close() 文件读取完成一定要close(),为了保证在报错的时候也能close(),这里用了finally语句,更简洁的写法是用with语句,会自动关闭。 with open(‘/p […]

Scroll to top