Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process a sequence of multiple temporary files in Python?

I want to go through the following steps:

1) tempfileA (Note: this is a blob downloaded from Google Cloud Storage)

2) tempfileB = function(tempfileA)

3) tempfileC = function(tempfileB)

This should be pretty straightforward, however, I am not sure about what's the best way to access different temporary files created in sequence based on the previous one.

So far I have found the example below from docs, but the Temporaryfile is closed at the exit of the with clause, so it should not be possible to access the temp file in the next step.

# create a temporary file using a context manager
with tempfile.TemporaryFile() as fp:
     fp.write(b'Hello world!')
     fp.seek(0)
     fp.read()

Could you please suggest what would be a good way to achieve what described above? Please note that at each step a method from an external library is called to process the current temp file and the result should be the next temp file.

like image 932
gorkamorka Avatar asked Mar 03 '23 14:03

gorkamorka


2 Answers

You can open multiple files in the same with block.

with TemporaryFile() as fp0, TemporaryFile() as fp1, TemporaryFile() as fp2:
    fp0.write(b'foo')
    fp0.seek(0)
    fp1.write(fp0.read())
    ...
like image 146
Håken Lid Avatar answered Apr 28 '23 07:04

Håken Lid


You can use a TemporaryDirectory and manually create files in there. For example:

import os
import tempfile

def process_file(f_name):
    with open(f_name) as fh:
        return fh.read().replace('foo', 'bar')

with tempfile.TemporaryDirectory() as td:
    f_names = [os.path.join(td, f'file{i}') for i in range(2)]
    with open(f_names[0], 'w') as fh:
        fh.write('this is the foo file')
    with open(f_names[1], 'w') as fh:
        fh.write(process_file(f_names[0]))
like image 34
a_guest Avatar answered Apr 28 '23 08:04

a_guest