1
Septembers 2015-04-26 15:29:48 +08:00
|
2
oclock 2015-04-26 20:18:13 +08:00
read()执行完就被gc,因为*open(from_file)*没有被引用
喜欢oneliner可以那么写 |
3
laoyuan OP @oclock 我感觉没有立刻回收呢,下面这三行print 地址都是相同的
from_file = 'sample.txt' print open(from_file) print open(from_file) indata = open(from_file).read() print open(from_file) |
4
Sylv 2015-04-27 02:48:30 +08:00 via iPhone
"Close is always necessary when dealing with files, it is not a good idea to leave open file handles all over the place. They will eventually be closed when the file object is garbage collected but you do not know when that will be and in the mean time you will be wasting system resources by holding to file handles you no longer need."
|
6
ryanking8215 2015-04-27 15:35:24 +08:00
|
7
czy1996 2015-05-25 22:56:29 +08:00
@ryanking8215 请教一下,还是不太明白,笨办法学python里说这样写的话read()一旦运行就被关掉了。
|