【发布时间】:2023-04-03 16:40:02
【问题描述】:
我正在生成以下 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:
....
如何重复使用我制作的临时文件?
【问题讨论】:
标签: python with-statement temporary-files contextmanager