【问题标题】:Cannot run command line command via Popen - "File not Found" (but which file?) [closed]无法通过 Popen 运行命令行命令 - \“找不到文件\”(但是 whxih 文件?)
【发布时间】:2022-12-07 18:33:23
【问题描述】:

我正在尝试通过 Python 中的命令行运行 tesseract。为此,我正在做:

import subprocess

file_full_path = '"C:\\Users\\me\\ml invoice\\server_tmp\\jpg\\my_file.pdf_0.jpg"'
output_file_name = '"C:\\Users\\me\\ml invoice\\server_tmp\\my_file.pdf_0"'
command = ["tesseract", file_full_path, output_file_name, "-l", "eng", "hocr"]

process = subprocess.Popen(command)
output, error = process.communicate()

如果执行 command = " ".join(bash_command) 并将命令复制粘贴到 CMD 中,命令运行正常。但是,上面的代码不会运行并产生错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Input In [14], in <cell line: 5>()
      2 output_file_name = '"C:\\Users\\me\\ml invoice\\server_tmp\\my_file.pdf_0"'
      3 bash_command = ["tesseract", file_full_path, output_file_name, "-l", "hun", "hocr"]
----> 5 process = subprocess.Popen(bash_command)
      6 output, error = process.communicate()

File C:\ProgramData\Anaconda3\lib\subprocess.py:951, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
    947         if self.text_mode:
    948             self.stderr = io.TextIOWrapper(self.stderr,
    949                     encoding=encoding, errors=errors)
--> 951     self._execute_child(args, executable, preexec_fn, close_fds,
    952                         pass_fds, cwd, env,
    953                         startupinfo, creationflags, shell,
    954                         p2cread, p2cwrite,
    955                         c2pread, c2pwrite,
    956                         errread, errwrite,
    957                         restore_signals,
    958                         gid, gids, uid, umask,
    959                         start_new_session)
    960 except:
    961     # Cleanup if the child failed starting.
    962     for f in filter(None, (self.stdin, self.stdout, self.stderr)):

File C:\ProgramData\Anaconda3\lib\subprocess.py:1420, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session)
   1418 # Start the process
   1419 try:
-> 1420     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1421                              # no special security
   1422                              None, None,
   1423                              int(not close_fds),
   1424                              creationflags,
   1425                              env,
   1426                              cwd,
   1427                              startupinfo)
   1428 finally:
   1429     # Child is launched. Close the parent's copy of those pipe
   1430     # handles that only the child should have open.  You need
   (...)
   1433     # pipe will not close when the child process exits and the
   1434     # ReadFile will hang.
   1435     self._close_pipe_fds(p2cread, p2cwrite,
   1436                          c2pread, c2pwrite,
   1437                          errread, errwrite)

FileNotFoundError: [WinError 2] The system cannot find the file specified

几天来我一直在尝试逐步调试,但是使用子进程使它成为一场噩梦。我不知道找不到什么文件和在哪里,因为我添加的所有文件和文件夹肯定都在那里 - 我已经四重检查了。

我如何找出 Python(或命令行)认为缺少的文件?我究竟做错了什么?

如果我打印_winapi.CreateProcess()收到的args,它包含正确的CMD命令:tesseract "C:\\Users\\me\\ml invoice\\server_tmp\\jpg\\my_file.pdf_0.jpg" "C:\\Users\\me\\ml invoice\\server_tmp\\my_file.pdf_0" -l eng hocr,那个文件是确实那里。

【问题讨论】:

  • 首先尝试提供 tesseract 二进制文件的完整路径。 (为什么称它为bash_command 并显示 windows traceback - 是 WSL 吗?纯 windows?请相应地调整标签)
  • 我正在使用其他人编写的代码,他们将其称为 bash_command 但他们没有给出使用 bash 的参数,因此它应该只是一个常规的 Windows cmd 命令。是的,添加 tesseract 的完整路径解决了它......这是否意味着在使用 Popen 时,它会忽略 PATH 变量?
  • @SUTerliakov 你应该写一个简短的答案! :)
  • 嵌套引号是错误的。如果您使用的是shell=True,则需要引用引号,但您没有。
  • Related:“此外,在 shell=False 的 Windows 上,Popen 根本不关注 PATH,只会查找相对于当前工作目录的内容。”

标签: python windows popen file-not-found


【解决方案1】:

你可以试试这个

import subprocess
import sys
from subprocess import  PIPE

file_full_path = "C:\Users\Aadesh\Downloads\play safe\20220425_EDA22.pdf"
output_file_name = "C:\Users\Aadesh\Downloads\play safe\20220425_EDA22________222.pdf"
command = ["tesseract", file_full_path, output_file_name, "-l", "eng", "hocr"]

process = subprocess.Popen([sys.executable or 'python'] + command, stdout = PIPE)
output, error = process.communicate()
print()

【讨论】:

  • 这是什么?.. 你为什么要用sys.executable调用tesseract(编译后的二进制文件)?
猜你喜欢
  • 2015-10-09
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2012-05-03
  • 1970-01-01
  • 2018-04-28
相关资源
最近更新 更多