[PyQt] UnicodeDecodeError with output from Windows OS command
jnbarchan at gmail.com
Thu Nov 30 11:02:57 GMT 2017
ear, helpful PyQt/Python experts,
I have a problem, receiving a UnicodeDecodeError. The full details are at
in combination with
I will try to be brief to cover only the salient points.:
1. I am spawning a QProcess under Windows to run an arbitrary OS
command, and grab its output to display in a window.
2. I use QByteArray Process.readAllStandardOutput() to receive the
child's output in the parent.
3. I wish to display it in a dialog window, so I (believe I need to) use
QLineEdit.setPlainText(QString) to do that.
4. To achieve that I will need to convert a QByteArray to a QString.
5. Since neither of these types and their methods are available in PyQt,
I asked how to do that in
6. The answer I got from a PyQt expert, "jazzycamel", who visited there
(and told me to come to this forum in future, which is why I am here now!),
was that the Python/PyQt way to do this is to work with its native bytes
& str types respectively, and use str = bytes.decode('utf-8') to
While this has worked fine under Linux, when a user runs my Qt app under
Windows and issues a perfectly normal robocopy command under a standard UK
Windows with really nothing special/unusual going on with filenames, he
> Unhandled Exception:
> 'utf-8' codec can't decode byte 0x9c in position 32: invalid start byte
> <class 'UnicodeDecodeError'>
> File "C:\HJinn\widgets\messageboxes.py", line 289, in
> output = output.data().decode('utf-8')
I understand this to be a Python/PyQt problem. I *believe* if I used the
native Qt/C++ calls for this --- which apparently I can't from PyQt ---
there would be no issue.
I have over the years written, say, Windows C programs using standard
Windows SDK calls for this kind of "redirector". I simply grab the output
of a sub-process and throw it at whatever the native Windows SetTextEdit()
function is, and all has always been fine.
Note that I have *never* had to guess/decode/convert bytes to some text
encoding, and this has worked across all platforms forever. So I really
don't expect to have to do so now, unless there's something going on in
Qt/PyQt which is fundamentally any different.
So... can you please tell me how under Python/PyQt I can just display the
output from an OS command (assuming "text-type" output, I don't expect
arbitrary binary bytes) without the slightest chance of any kind of "I
can't convert" Exception, please?
Thank you in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the PyQt