我想创建一个临时文件,我从另一个文件的某些行写入,然后从数据中创建一些对象。我不知道如何找到并打开临时文件,以便我可以阅读它。我的代码:Python - 写入和读取临时文件
with tempfile.TemporaryFile() as tmp:
lines = open(file1).readlines()
tmp.writelines(lines[2:-1])
dependencyList = []
for line in tmp:
groupId = textwrap.dedent(line.split(':')[0])
artifactId = line.split(':')[1]
version = line.split(':')[3]
scope = str.strip(line.split(':')[4])
dependencyObject = depenObj(groupId, artifactId, version, scope)
dependencyList.append(dependencyObject)
tmp.close()
本质上,我只是想做一个中间人临时文件,以防止意外覆盖文件。
我从来没有使用的临时文件,是否有你arn't使用标准'的open()''write'和'read'方法的原因吗? – kpie
我想防止文件名已存在的可能性,我可以覆盖它 –
1.您是否考虑过简单地将输出从一个脚本输送到第二个脚本的输入? 2.您是否正在检查以确保临时文件存在于您正在查找的路径中? – erapert