简体   繁体   中英

How to run a CLI command from a Python script?

When I write this in CMD (Windows 10) it's OK. The recognized text is in the clipboard: "C: \\ Program Files (x86) \\ ABBYY FineReader 15 \\ FineReaderOcr.exe" "C: \\ Python39 \\ Scripts \\ abbyy \\ Skan.JPG" / send Clipboard

I would like to do the same with a Python script to be able to parse the contents of the clipboard. 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.

I also tried using 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.

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.

File paths with spaces and \\u\u003c/i>

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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