huihuimm

博客园 首页 联系 订阅 管理
###
这段代码是用于读文件、写文件
###
from sys import argv
from os.path import exists

script, from_file, to_file = argv
#获取文件内容
in_file = open(from_file)
indata = in_file.read()

#将文件内容写入到目标文件
if exists(to_file) <> 0:
    open(to_file,"w").write(indata)
  
#结束
in_file.close()

1、open(),close(),write(T),exists(file)

什么时候需要close()?

当open().read()时,会自动调close(),此时不用再次close();

当in_file= open(file)时,需要使用close()关闭文件,否则容易造成文件坏

posted on 2022-11-12 11:18  huihuimm  阅读(25)  评论(0)    收藏  举报