CalledProcessError:命令'['pdftotext','-layout','coburns.pdf','-']'返回非零退出状态1?

时间:2020-05-02 14:58:17

标签: python error-handling subprocess pdftotext re

运行代码的这一部分时,我总是收到错误消息。我已经尝试按照universal_newlines=True文档的建议实施subprocess.check_output,但是遇到了同样的错误。为什么会发生这种情况,我该怎么解决?

for filename in os.listdir(directory):
    if filename in new_list:
        pdf = filename
        output = subprocess.check_output(['pdftotext', '-layout', pdf, '-'], universal_newlines=True).decode()
        pages = output.strip('\f').split('\f')
        page = pages[-1]

错误消息:

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-153-adc4a58d7f21> in <module>
      9     if filename in new_list:
     10         pdf = filename
---> 11         output = subprocess.check_output(['pdftotext', '-layout', pdf, '-'], universal_newlines=True).decode()
     12         pages = output.strip('\f').split('\f')
     13         page = pages[-1]

/usr/lib/python3.8/subprocess.py in check_output(timeout, *popenargs, **kwargs)
    409         kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
    410 
--> 411     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
    412                **kwargs).stdout
    413 

/usr/lib/python3.8/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    510         retcode = process.poll()
    511         if check and retcode:
--> 512             raise CalledProcessError(retcode, process.args,
    513                                      output=stdout, stderr=stderr)
    514     return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['pdftotext', '-layout', 'coburns.pdf', '-']' returned non-zero exit status 1.

1 个答案:

答案 0 :(得分:0)

如评论中所述,check_output实际上是在过程返回的代码不是$(document).ready(function() { $.get("https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Ftimesofindia.indiatimes.com%2Frssfeeds%2F-2128816011.cms&api_key=mkzfaqsb5gtninv1pddoke81vwmjlyfiissiitvd", function(data, status) { //$("#rowview").empty(); for (i in data.items) { console.log(data.items[i].thumbnail); console.log(data.items[i].title); console.log(data.items[i].link); console.log(data.items[i].description); console.log(data.items[i].description.slice(pos + 4)); var pos = data.items[i].description.indexOf("</a>"), $divitem = $("</div>").attr('class', 'item grid-group-item col-xs-4 col-lg-4 col-md-6 col-sm-12'); var $divthumb = $("</div>").attr({ "class": 'thumbnail card border-light', style: 'width: 20rem' }), $divimg = $("</div>").attr('class', 'img-event '), $imggrp = $("</img>").attr({ "class": 'group list-group-image img-fluid ', src: data.items[i].thumbnail }), $divcaption = $("</div>").attr('class', 'caption card-body'), $headcard = $("</h4>").text(data.items[i].title).attr('class', 'group card-title inner list-group-item-heading'), $paracard = $("</p>").attr('class', 'group inner list-group-item-text').text(data.items[i].description.slice(pos + 4)), $divrow = $("</div>").attr('class', 'row'), $divfooter1 = $("</div>").attr({ "class": 'col-xs-12 col-md-6', id: 'footer1' }), $lead = $("</p>").attr('class', 'lead').text('$21.000'), $divfooter2 = $("</div>").attr({ "class": 'col-xs-12 col-md-6', id: 'footer2' }), $atag = $("</a>").attr({ class: 'btn btn-success', href: 'http://www.jquery2dotnet.com' }).text("Add to cart"); $("#rowview").append($divitem.append($divthumb.append([$divimg.append($imggrp), $divcaption.append([$headcard, $paracard, $divrow.append([$divfooter1.append($lead), $divfooter2.append($atag)])])]))); // $($divthumb).append([$divimg, $divcaption]); //$($divimg).append($imggrp); // $(divthumb).append(divcaption); //$($divcaption).append([$headcard, $paracard, $divrow]); // $(divcaption).append(paracard); //$(divcaption).append(divrow); //$($divrow).append([$divfooter1, $divfooter2]); //$($divfooter1).append($lead); //$(divrow).append(divfooter2); // $($divfooter2).append($atag); // $(".view-group").append($divitem); } }); }); 时引发CalledProcessError

此处的代码为0,表示1无法打开pdf,原因可能是它不正确或因为找不到它。

您正在使用os.listdir,它产生的文件名相对于pdftotext参数,它不一定与directory将在其中启动{{ 1}}。

我可以看到三种解决方案: