0
votes

I am getting this error

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

here is what I want to achieve.

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')
    [..]

I am new to Django and python, I have tried many solutions but nothing is working for me. because I don't know how it works.

Environment:

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

Django Version: 3.1.2
Python Version: 3.7.4

here is the full traceback.

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.

the problem is with the check_output function of the subprocess. I don't know how to rectify this issue.

1

1 Answers

0
votes

I can't comment, so I can't ask for clarification, but I believe you are trying to run a subprocess that runs a shell command, the name of which is contained within Django's settings.PDF_TO_TEXT variable, and run it on the file absolute_file_path. If so, those two variables shouldn't be in quotes in the subprocess.call arguments:

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')
    ...

The reason here is that subprocess.check_output (and its underlying subprocess.run) do not take strings and parse them into Python variables. The Python parser will first convert the variables settings.PDF_TO_TEXT and absolute_file_path into strings (I assume that's what the variables hold), and then that list of strings will be passed to subprocess.check_output, which just carries them to the shell.