Python - 写入和读取临时文件
Python - writing and reading from a temporary file
我正在尝试创建一个临时文件,我从另一个文件中写入一些行,然后从数据中创建一些对象。我不确定如何找到并打开临时文件以便阅读。我的代码:
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()
基本上我只是想制作一个中间人临时文件以防止意外覆盖文件。
根据 docs,文件在 TemporaryFile
关闭时被删除,当您退出 with
子句时会发生这种情况。所以...不要退出 with
子句。倒回文件并在 with
中完成您的工作。
with tempfile.TemporaryFile() as tmp:
lines = open(file1).readlines()
tmp.writelines(lines[2:-1])
tmp.seek(0)
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
仅存在于创建它的 with
语句的范围内。此外,如果您想稍后在初始 with
之外访问该文件,则需要使用 NamedTemporaryFile
(这使 OS 能够访问该文件)。另外,我不确定您为什么要尝试附加到临时文件...因为它在您实例化之前不存在。
试试这个:
import tempfile
tmp = tempfile.NamedTemporaryFile()
# Open the file for writing.
with open(tmp.name, 'w') as f:
f.write(stuff) # where `stuff` is, y'know... stuff to write (a string)
...
# Open the file for reading.
with open(tmp.name) as f:
for line in f:
... # more things here
以防需要再次打开文件,例如通过不同的进程读取此 might cause trouble on Windows OS:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
因此,一个安全的解决方案是 create a temporary directory,然后在其中手动创建一个文件:
import os.path
import tempfile
with tempfile.TemporaryDirectory() as td:
f_name = os.path.join(td, 'test')
with open(f_name, 'w') as fh:
fh.write('<content>')
# Now the file is written and closed and can be used for reading.
我正在尝试创建一个临时文件,我从另一个文件中写入一些行,然后从数据中创建一些对象。我不确定如何找到并打开临时文件以便阅读。我的代码:
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()
基本上我只是想制作一个中间人临时文件以防止意外覆盖文件。
根据 docs,文件在 TemporaryFile
关闭时被删除,当您退出 with
子句时会发生这种情况。所以...不要退出 with
子句。倒回文件并在 with
中完成您的工作。
with tempfile.TemporaryFile() as tmp:
lines = open(file1).readlines()
tmp.writelines(lines[2:-1])
tmp.seek(0)
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
仅存在于创建它的 with
语句的范围内。此外,如果您想稍后在初始 with
之外访问该文件,则需要使用 NamedTemporaryFile
(这使 OS 能够访问该文件)。另外,我不确定您为什么要尝试附加到临时文件...因为它在您实例化之前不存在。
试试这个:
import tempfile
tmp = tempfile.NamedTemporaryFile()
# Open the file for writing.
with open(tmp.name, 'w') as f:
f.write(stuff) # where `stuff` is, y'know... stuff to write (a string)
...
# Open the file for reading.
with open(tmp.name) as f:
for line in f:
... # more things here
以防需要再次打开文件,例如通过不同的进程读取此 might cause trouble on Windows OS:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
因此,一个安全的解决方案是 create a temporary directory,然后在其中手动创建一个文件:
import os.path
import tempfile
with tempfile.TemporaryDirectory() as td:
f_name = os.path.join(td, 'test')
with open(f_name, 'w') as fh:
fh.write('<content>')
# Now the file is written and closed and can be used for reading.