python文件处理

  开发

49728d320f41e459.jpg
文件处理

file = open("/tmp/foo.txt")
data = file.read()
file.close()

处理异常

file = open("/tmp/foo.txt")
try:
    data = file.read()
finally:
    file.close()

with可以很好的处理上下文环境产生的异常

with open("/tmp/foo.txt") as file:
    data = file.read()

LEAVE A COMMENT

Captcha Code