子进程调用python脚本不生成Jasper报告但cmd执行

时间:2014-03-16 00:43:30

标签: python jasper-reports subprocess

当我在命令行运行它时,它会正确生成我的Jasper报告:

jasperstarter pr "C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml" -f pdf -t postgres -H localhost -n template_postgis_20 -u postgres -p postgres -P SiteID=123

但是,如果我尝试使用以下代码通过python运行它,则不会创建报告。我在某处弄乱了语法吗?

import subprocess
from subprocess import call

subprocess.call(["cmd","/C","jasperstarter","pr","""C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml""","-f","pdf",
"-t","postgres","-H","localhost","-n","template_postgis_20","-u","postgres","-p","postgres",
"-P","SiteID=123"], shell=True)

修改

在评论之后,我尝试在输入python后在cmd运行此命令以显示>>>:

jasperstarter pr "C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml" -f pdf -t postgres -H localhost -n template_postgis_20 -u postgres -p postgres -P SiteID=123

这次我在-u遇到语法错误。然后我尝试重新排序参数,然后语法错误发生在相同的字符编号,而不是在-u。那么在cmd中在python中输入命令时是否有最大行长度?

1 个答案:

答案 0 :(得分:1)

\a是一个与\x07(BEL)相同的转义序列。您应该转义\或使用原始字符串文字使\a字面上代表\a

>>> '\a' # not escaped
'\x07'
>>> '\\a' # escaped
'\\a'
>>> r'\a' # raw string literal
'\\a'

所以,请替换以下内容:

"""C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml"""

"""C:\\users\\ant\\jaspersoftworkspace\\myreports\\carereport.jrxml"""

r"""C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml"""

<强>更新

请尝试以下操作:

subprocess.call(r'jasperstarter pr "C:\users\ant\jaspersoftworkspace\myreports\carereport.jrxml" -f pdf -t postgres -H localhost -n template_postgis_20 -u postgres -p postgres -P SiteID=123', shell=True)