将python中的subprocess.popen与os.tmp文件一起使用,同时传递可选参数
我正在linux中编写一个python程序,其中一部分运行pdftotext可执行文件来转换pdf文本。下面给出了我当前使用的代码将python中的subprocess.popen与os.tmp文件一起使用,同时传递可选参数,python,linux,pdftotext,Python,Linux,Pdftotext,我正在linux中编写一个python程序,其中一部分运行pdftotext可执行文件来转换pdf文本。下面给出了我当前使用的代码 pdfData = currentPDF.read() tf = os.tmpfile() tf.write(pdfData) tf.seek(0) out, err = subprocess.Popen(["pdftotext", "-", "-"], stdin = tf, stdout=subprocess.PIPE ).communicate() 这很
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-layout”替换“pdftotext-layout”等等,但都不起作用。他们都给我发了一条空短信。由于输入是通过temp文件传入的,所以我很难理解参数列表。关于Popen的大多数文档都假定所有参数都是通过参数列表传入的,但在我的例子中,输入是通过temp文件传入的
非常感谢您的帮助。您可以通过shell=True字符串传递完整的命令:
out, err = subprocess.Popen('pdftotext -layout - -', shell=True, stdin=tf, stdout=subprocess.PIPE).communicate()
这对我很有用:
out, err = subprocess.Popen(
["pdftotext", '-layout', "-", "-"], stdin = tf, stdout=subprocess.PIPE ).communicate()
虽然我在手册页中找不到明确的确认,但我相信第一个-
告诉pdftotext
希望PDF文件
来自stdin,第二个-
告诉pdftotext
希望文本文件
发送到stdout。不起作用。我收到一个错误“TypeError:“Popen”对象不可编辑”