【发布时间】:2016-01-03 10:08:36
【问题描述】:
我有以下 Python 脚本。我注意到每次read() 或write() 之后我都必须open() 文件。是不是因为这样操作后文件会自动关闭?
text_file = open('Text.txt','r')
print 'The file content BEFORE writing content:'
print text_file.read()
text_file = open('Text.txt','a')
text_file.write(' add this text to the file')
print 'The file content AFTER writing content:'
text_file = open('Text.txt','r')
print text_file.read()
谢谢。
【问题讨论】:
-
不,您不需要多次打开文件。