简体   繁体   English

如何从 Python 脚本运行 CLI 命令?

[英]How to run a CLI command from a Python script?

When I write this in CMD (Windows 10) it's OK.当我在 CMD (Windows 10) 中写这个时,没关系。 The recognized text is in the clipboard: "C: \\ Program Files (x86) \\ ABBYY FineReader 15 \\ FineReaderOcr.exe" "C: \\ Python39 \\ Scripts \\ abbyy \\ Skan.JPG" / send Clipboard识别的文本在剪贴板中:“C:\\Program Files (x86)\\ABBYY FineReader 15\\FineReaderOcr.exe”“C:\\Python39\\Scripts\\abbyy\\Skan.JPG”/发送剪贴板

I would like to do the same with a Python script to be able to parse the contents of the clipboard.我想用 Python 脚本做同样的事情,以便能够解析剪贴板的内容。 I am trying to do this using:我正在尝试使用以下方法执行此操作:

import sys
import os
def mycmd():
  os.system('cmd /c "C:\Program Files (x86)"\ABBYY FineReader 15\FineReaderOcr.exe" "skan.JPG" " /send Clipboard"')
mycmd()

But it doesn't work and you get the following error: 'C: \\ Program' is not recognized as an internal or external command, operable program or batch file.但它不起作用,您会收到以下错误:“C:\\Program”不是内部或外部命令,也不是可运行的程序或批处理文件。

I also tried using subprocess: import of os我也尝试使用 subprocess: import of os

import subprocess
program = 'C: \ Program Files (x86) \ ABBYY FineReader 15 \ FineReaderOcr.exe'
file = 'C: \ Python39 \ Scripts \ abbyy \ Skan.JPG'
lang = "/ lang Polish"
send = "/ send Clipboard"
subprocess.run (["C: \ Program Files (x86) \ ABBYY FineReader 15 \ FineReaderOcr.exe", "scan.JPG", "/ send Clipboard"])
subprocess.call ([program, file, send])

But it also doesn't work as it should.但它也不能正常工作。 Please tell me why it doesn't work and how to do it correctly?请告诉我为什么它不起作用以及如何正确执行?

regards问候

Whitespace is important.空格很重要。

The space within Program Files is making that first file path be interpreted as two separate arguments, and it doesn't know how to interpret the first one, C:\\Program as a command. Program Files的空间使第一个文件路径被解释为两个单独的参数,并且它不知道如何将第一个C:\\Program为命令。

I've made a few similar examples to illustrate what works and what doesn't:我做了一些类似的例子来说明什么有效,什么无效:

File paths that use no spaces不使用空格的文件路径

>>> import os
>>> os.system('C:\ProgramData\Miniconda3\python.exe --version')
Python 3.9.1
0
>>> os.system('cmd /c "C:\ProgramData\Miniconda3\python.exe --version"')
Python 3.9.1
0
>>> os.system(r'cmd /c "C:\ProgramData\Miniconda3\python.exe --version"')
Python 3.9.1
0

These 3 versions are all successful because C:\\ProgramData\\Miniconda3\\python.exe , the file path in question, has no spaces in it.这三个版本都成功了,因为C:\\ProgramData\\Miniconda3\\python.exe ,有问题的文件路径,里面没有空格。

File paths with spaces and \\u\u003c/i>带空格和 \\u 的文件路径

This command doesn't work:此命令不起作用:

>>> os.system('C:\Program Files (x86)\LilyPond\usr\bin\python.exe --version')
  File "<stdin>", line 1
    os.system('C:\Program Files (x86)\LilyPond\usr\bin\python.exe --version')
                                                                            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 31-32: truncated \uXXXX escape

because it contains \\u\u003c/code> (in ...\\usr\\... ).因为它包含\\u\u003c/code> (在...\\usr\\... )。 When you use \\u\u003c/code> as part of a string, python expects it to be a part of a Unicode escape sequence (eg print("\’") will get you a special apostrophe)当您使用\\u\u003c/code>作为字符串的一部分时,python 期望它是 Unicode 转义序列的一部分(例如print("\’")会给您一个特殊的撇号)

The way to fix this is to either escape your backslashes with another backslash (eg 'C:\\\\ProgramData\\\\...' ) or by making it a "raw" string with a prefixed r : r'C:\\ProgramData\\...'解决此问题的方法是使用另一个反斜杠(例如'C:\\\\ProgramData\\\\...' )转义您的反斜杠,或者使其成为带有前缀r“原始”字符串r'C:\\ProgramData\\...'

That gets us here:这让我们在这里:

>>> os.system(r'C:\Program Files (x86)\LilyPond\usr\bin\python.exe --version')
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
1

which is the problem I initially described;这是我最初描述的问题; spaces are used to break up commands/arguments and cannot plainly be included in file paths.空格用于分解命令/参数,不能简单地包含在文件路径中。

The solution is to put quotes around the whole file path which contains a space:解决方案是在包含空格的整个文件路径周围加上引号:

>>> os.system(r'"C:\Program Files (x86)\LilyPond\usr\bin\python.exe" --version')
Python 3.7.4
0

These same things apply when the command is being sent as an argument:当命令作为参数发送时,这些相同的事情适用:

>>> os.system(r'cmd /c "C:\Program Files (x86)\LilyPond\usr\bin\python.exe --version"')
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
1
>>> os.system(r'cmd /c ""C:\Program Files (x86)\LilyPond\usr\bin\python.exe" --version"')
Python 3.7.4
0

Conclusion结论

So it seems that your line should look something like this:所以看起来你的行应该是这样的:

os.system(r'cmd /c ""C:\Program Files (x86)\ABBYY FineReader 15\FineReaderOcr.exe" skan.JPG" /send Clipboard')

(though I don't know what the /send Clipboard part is for, and so may need adjustment from this. However, this should address the error asked about in the question) (虽然我不知道/send Clipboard部分的用途,因此可能需要对此进行调整。但是,这应该解决问题中询问的错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM