Python:CalledProcessError,命令返回非零退出状态1

2024-05-13 06:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个错误

Command '['settings.PDF_TO_TEXT', '-layout', 'absolute_file_path', '-']' returned non-zero exit status 1.

这就是我想要实现的目标

def get_queries(filename, num_queries=3):
    scored_chunks = []
    absolute_file_path = os.path.join(settings.MEDIA_ROOT, filename)
    # pdf_to_text_output = subprocess.check_output([settings.PDF_TO_TEXT, "-layout", absolute_file_path, "-"], shell=true)
    pdf_to_text_output = subprocess.check_output(['settings.PDF_TO_TEXT', "-layout", 'absolute_file_path', "-"], shell=True)
    try:
        text = pdf_to_text_output.decode('utf-8')
    except UnicodeDecodeError:
        text = pdf_to_text_output.decode('ISO-8859-1')
    [..]

我是Django和python的新手,我尝试过很多解决方案,但都不管用。因为我不知道它是怎么工作的

环境:

Request Method: POST
Request URL: http://127.0.0.1:8000/index/

Django Version: 3.1.2
Python Version: 3.7.4

这是完整的回溯

Traceback (most recent call last):
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, 
    in inner response = get_response(request)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 179, 
    in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 70, 
    in view return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 98, 
    in dispatch return handler(request, *args, **kwargs)
  File "C:\Users\Farhana Noureen\plagtrap\main\views.py", line 302, 
    in post results = process_homepage_trial(request)
  File "C:\Users\Farhana Noureen\plagtrap\main\services.py", line 435, 
    in process_homepage_trial queries = pdf.get_queries(filename)
  File "C:\Users\Farhana Noureen\plagtrap\util\getqueriespertype\pdf.py", line 18, 
    in get_queries pdf_to_text_output = subprocess.check_output(['settings.PDF_TO_TEXT', "-layout", 'absolute_file_path', "-"], shell=True)
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 395, 
    in check_output **kwargs).stdout
  File "C:\Users\Farhana Noureen\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 487, 
    in run output=stdout, stderr=stderr)

  Exception Type: CalledProcessError at /index-trial/
  Exception Value: Command '['settings.PDF_TO_TEXT', '-layout', 'absolute_file_path', '-']' returned non-zero exit status 1.

问题在于子流程的check_输出函数。我不知道如何纠正这个问题


Tags: pathtextinpyoutputsettingspdfline
1条回答
网友
1楼 · 发布于 2024-05-13 06:21:24

我不能评论,所以我不能要求澄清,但我相信您正在尝试运行一个运行shell命令的子进程,该命令的名称包含在Django的settings.PDF_TO_TEXT变量中,并在文件absolute_file_path上运行它。如果是,这两个变量不应在subprocess.call参数中加引号:

def get_queries(filename, num_queries=3):
    scored_chunks = []
    absolute_file_path = os.path.join(settings.MEDIA_ROOT, filename)
    pdf_to_text_output = subprocess.check_output([
        settings.PDF_TO_TEXT,
        "-layout",
        absolute_file_path,
        "-",
    ], shell=True)
    try:
        text = pdf_to_text_output.decode('utf-8')
    except UnicodeDecodeError:
        text = pdf_to_text_output.decode('ISO-8859-1')
    ...

这里的原因是subprocess.check_output(及其底层subprocess.run)不接受字符串并将其解析为Python变量。Python解析器将首先将变量settings.PDF_TO_TEXTabsolute_file_path转换为字符串(我假设变量就是这样保存的),然后字符串列表将被传递给subprocess.check_output,后者只将它们传递给shell

相关问题 更多 >