Run command line from python and piping arguments from memory

I was wondering if there is a way to run the command line executable in python, but pass it the argument values ​​from memory without writing the memory data to a temporary file on disk. From what I saw, it seems that subprocess.Popen (args) is the preferred way to run programs from within python scripts.

For example, I have a pdf file in memory. I want to convert it to text using the pdftotext command line function, which is present on most linux distributions. But I would prefer not to write the pdf file in memory to a temporary file on disk.

pdfInMemory = myPdfReader.read() convertedText = subprocess.<method>(['pdftotext', ??]) <- what is the value of ?? 

What is the method that I should name, and how should I transfer data to memory in my first input and output its output back to another variable in memory?

I guess there are other PDF modules that can do the in-memory conversion, and information about these modules would be helpful. But for future reference, I'm also interested in how to connect input and output to the command line from within python.

Any help would be greatly appreciated.

+4
source share
3 answers

from Popen.communicate :

 import subprocess out, err = subprocess.Popen(["pdftotext", "-", "-"], stdout=subprocess.PIPE).communicate(pdf_data) 
+1
source

os.tmpfile is useful if you need a thing to search for. It uses the file, but it is almost as simple as the approach to the pipe, there is no need for cleaning.

 tf=os.tmpfile() tf.write(...) tf.seek(0) subprocess.Popen( ... , stdin = tf) 

This may not work with Windows without an OS.

+2
source

Popen.communicate from the subprocess takes an input parameter that is used to send data to stdin, you can use this to enter your data. You also get the output of your program from communicate , so you do not need to write it to a file.

The documentation for the message explicitly warns that everything is buffered in memory, which seems to be exactly what you want to achieve.

+1
source

All Articles