Skip to main content
 首页 » 编程设计

python,打开的文件会被关闭吗

2025年05月04日11zfyouxi

<分区>

Possible Duplicate:
Does filehandle get closed automatically in Python after it goes out of scope?

我是 python 新手。我知道如果你打开一个文件并写入它,你需要在最后关闭它。

in_file = open(from_file) 
indata = in_file.read() 
 
out_file = open(to_file, 'w') 
out_file.write(indata) 
 
out_file.close() 
in_file.close() 

如果我这样写我的代码。

open(to_file, 'w').write(open(from_file).read()) 

真的不能关闭,会自动关闭吗?