小编Inf*_*ite的帖子

为什么要关闭文件

from sys import argv
from os.path import exists

script, from_file, to_file = argv
print "Copying fro %s to %s" % (from_file, to_file)


in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to countinue, CRL-C to abort."
raw_input("?")

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()
Run Code Online (Sandbox Code Playgroud)

有必要写out_file.close()in_file.close()吗?

如果我不写最后两行,仍然可以使用。同时关闭(out_file,和in_file)有什么区别?

python string python-2.7

-2
推荐指数
1
解决办法
733
查看次数

标签 统计

python ×1

python-2.7 ×1

string ×1