繁体   English   中英

TemporaryFileWrapper实例没有__call__方法

[英]TemporaryFileWrapper instance has no __call__ method

我正在生成以下NamedTemporaryFile-

## CONFIGURE DEPLOY.XPR
template = open(xprpath + xprtemplatefile, 'r')
joblist = open(joblistfilepath + joblistfilename, 'r')
temp = NamedTemporaryFile(delete=False)
data = template.read()
listjobs = joblist.read()
template.close()
joblist.close()

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text
values = {'<srcalias>':srcalias, '<dstalias>':dstalias}
data = replace_all(data, values)
temp.write(data)
temp.write("\n")
temp.write(listjobs)
temp.seek(0)

然后,我想在这里的另一部分代码中使用它-

with temp() as f:
    count = 1
    for line in f:
        equal = '='
        if (str(count) + equal) in line:   
....

如何重新使用已创建的临时文件?

您不必调用它:

with temp as f:
    count = 1
    for line in f:

或简单地

with temp:
    count = 1
    for line in temp:

该对象已经是上下文管理器。 您一定已经将它与open()混淆了,在该函数中该函数的调用会产生一个新的文件对象,然后将该文件对象用作上下文管理器。

考虑到with语句的末尾, temp文件对象将被关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM