1

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

1

1 Answer 1

5

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

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 (in ...\usr\...). When you use \u as part of a string, python expects it to be a part of a Unicode escape sequence (e.g. print("\u2019") will get you a special apostrophe)

The way to fix this is to either escape your backslashes with another backslash (e.g. '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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the extensive explanation. The last command works exactly as I expected, i.e. it runs ocr on the image and copies the result to the clipboard. Why is this code not working? (CLI) set image = scan.jpg cmd / c "" C: \ Program Files (x86) \ ABBYY FineReader 15 \ FineReaderOcr.exe ""% file% / send Clipboard Is it possible to substitute the variable 'image' here instead of the name 'scan.jpg'? How to use os.system and subprocess in this example? regards
Check out tutorials for formatting strings in python: geeksforgeeks.org/string-formatting-in-python
If this answered your question, please mark as accepted answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.