python 无法通过Popen运行命令行命令-“找不到文件”(但是哪个文件?)[已关闭]

jjjwad0x  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(362)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
2天前关闭。
Improve this question
我尝试在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,而且该文件 * 肯定 * 在那里。
编辑:这个问题被关闭为“由一个错字或不可复制”。我认为这是不正确的,因为它是绝对可复制的,它的解决方案不是“纠正一个错字”,而是增加了实际有用的知识,即“Popen忽略了PATH变量”,这是以前未知的,至少对我来说。这个关闭是没有保证的。

ev7lccsx

ev7lccsx1#

你可以试试这个

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()

相关问题