[英]Python - writing and reading from a temporary file
I am trying to create a temporary file that I write in some lines from another file and then make some objects from the data. 我正在尝试创建一个临时文件,我在另一个文件的某些行中编写,然后从数据中创建一些对象。 I am not sure how to find and open the temp file so I can read it.
我不知道如何找到并打开临时文件,以便我可以阅读它。 My code:
我的代码:
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()
Essentially I just want to make a middleman temporary document to protect against accidentally overwriting a file. 基本上我只是想制作一个中间人临时文件,以防止意外覆盖文件。
As per the docs , the file is deleted when the TemporaryFile
is closed and that happens when you exit the with
clause. 根据文档 ,当
TemporaryFile
关闭时将删除该文件 ,并在退出with
子句时删除该文件。 So... don't exit the with
clause. 所以...不要退出
with
子句。 Rewind the file and do your work in the 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)
You've got a scope problem; 你有一个范围问题; the file
tmp
only exists within the scope of the with
statement which creates it. 文件
tmp
仅存在于创建它的with
语句的范围内。 Additionally, you'll need to use a NamedTemporaryFile
if you want to access the file later outside of the initial with
(this gives the OS the ability to access the file). 此外,您还需要使用一个
NamedTemporaryFile
如果您要访问的文件后外最初的with
(这使OS访问文件的能力)。 Also, I'm not sure why you're trying to append to a temporary file... since it won't have existed before you instantiate it. 此外,我不确定你为什么要尝试附加到临时文件...因为它在你实例化之前不会存在。
Try this: 试试这个:
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
In case the file needs to be opened a second time, eg read by a different process this might cause trouble on Windows OS : 如果文件需要第二次打开,例如由不同的进程读取,这可能会导致Windows操作系统出现问题 :
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).
名称是否可以用于第二次打开文件,而命名的临时文件仍然是打开的,因此不同平台(它可以在Unix上使用;它不能在Windows NT或更高版本上使用)。
Hence a safe solution is to create a temporary directory instead and then manually create a file therein: 因此,安全的解决方案是创建一个临时目录 ,然后在其中手动创建一个文件:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.