using subprocess.popen in python with os.tmp file while passing in optional parameters
我在linux中编写一个python程序,部分运行pdftotext可执行文件以转换pdf文本。 我当前使用的代码如下。
1 2 3 4 5 6 7 | pdfData = currentPDF.read() tf = os.tmpfile() tf.write(pdfData) tf.seek(0) out, err = subprocess.Popen(["pdftotext","-","-"], stdin = tf, stdout=subprocess.PIPE ).communicate() |
这可以正常工作,但是现在我想使用-layout选项运行pdftotext可执行文件(保留文档的布局)。 我尝试将"-"替换为布局,将" pdftotext"替换为" pdftotext -layout"等。均无效。 他们都给我一个空白的文字。 由于输入是通过临时文件通过管道输入的,因此我很难弄清参数列表。 Popen上的大多数文档都假定所有参数都通过参数列表传递,但是在我的情况下,输入是通过临时文件传递的。
任何帮助将不胜感激。
这对我有用:
1 2 | out, err = subprocess.Popen( ["pdftotext", '-layout',"-","-"], stdin = tf, stdout=subprocess.PIPE ).communicate() |
尽管在手册页中找不到明确的确认信息,但我相信第一个
您可以使用shell = True以字符串形式传递完整命令:
1 | out, err = subprocess.Popen('pdftotext -layout - -', shell=True, stdin=tf, stdout=subprocess.PIPE).communicate() |