0

I have a code that uses two independent packages (let's call them packageA and packageB). PackageA has a function write(outputPath: str, ...) that writes a ".pdf" to disk of some data. PackageB includes a method called read(inputPath: str, ...) that reads a pdf and extracts some information. The workflow makes me have to write a temporary file with the function of packageA, read it with the function of packageB and then delete it and continue working with different tools of packageB.

When I run my program on my local machine, everything works correctly. However, I need to run this code on an external server where I cannot write to disk. What are my options? I have tried to write the file to memory using the tempfile package, but I have not succeeded because the write function of packageA needs a string as argument where it will create the "pdf". Any help?

EDIT: here is a minimum example, the two packages are actually music21 and PyMuPdf. In order to reproduce the example, apart from both packages you would need to install musescore, as the write methods does an internal call to it (via os)

import music21
import fitz
import os

piece = music21.corpus.parse('bwv66.6')

piece.write("musicxml.pdf", fp="temp.pdf")

with fitz.open("temp.pdf") as pdf:
    # do some stuff with 'pdf'

    pdf.save("final.pdf")

os.system("rm temp.pdf")
Kikolo
  • 212
  • 1
  • 10
  • 2
    `tempfile` is a regular file, memory backed or not. It should work. If you are using the context manager method it probably creates the file, then deletes it before you can pass it to `PackageB`. Can you post your code? – Selcuk Feb 24 '23 at 01:16
  • Thank you for your comment. I have edited the question to include a minimal example of the code – Kikolo Feb 24 '23 at 02:49
  • How do you use the `tempfile` module? You should be doing something like `, fp= tempfile.gettempdir() + "/temp.pdf"` and then `fitz.open(tempfile.gettempdir() + "/temp.pdf")...`. – Selcuk Feb 24 '23 at 04:01
  • I was trying to use `tempfile.SpooledTemporaryFile`, as it has the `max_size` parameter that allows me to make sure the file is handled in memory — I need this, as I explain in the question. Your solution using `tempfile.gettempdir()` works locally, but I suspect that the file is still stored on disk (just in a different path, reserved for temporary files). Is that so? Thx! – Kikolo Feb 27 '23 at 00:06

0 Answers0