Python - writing and reading from a temporary file

I am trying to create a temporary file that I write on some lines from another file and then create some objects from the data. I am not sure how to find and open a temporary file so that I can read it. My code is:

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 temporary intermediary document to protect against accidentally overwriting a file.

+15
source share
3 answers

According to docs, the file is deleted when the TemporaryFile closed and this happens when you exit the with clause. So ... don't leave the with clause. Rewind the file and do your job in 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) 
+18
source

You have a problem with the area; The tmp file exists only as part of the with statement that creates it. In addition, you will need to use NamedTemporaryFile if you want to access the file later outside the initial with (this gives the OS the ability to access the file). Also, I'm not sure why you are trying to add to a temporary file ... since it did not exist before you create it.

Try the following:

 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 
+17
source

If you need to open the file a second time, for example, read it by another process, this can cause problems on Windows :

The ability to use a name to open a file the second time the named temporary file is still open depends on the platform (it can be used on Unix; this is not possible on Windows NT or later).

Therefore, a safe solution is to create a temporary directory and then manually create a file in it:

 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. 
0
source

Source: https://habr.com/ru/post/1258044/


All Articles