1

I'm trying to execute a .vbs script from my python script using os.system().

It's like this

# -*-coding:utf-8 -*-
import os
os.system('cscript myVbsScript.vbs -i "This is a test" "1000" "æøå"')

"æøå" is not passed correctly.

When using:

os.system(u'cscript myVbsScript.vbs -i "This is a test" "1000" "æøå"')

I get the error:

os.system(u'cscript myVbsScript.vbs 10000 "This is a test" "1000" "æøå"')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 55-57: ordinal not in range(128)`

When I'm not trying to use "æøå" in os.system then it works like a charm - so I am afraid that the problem is with os.system.

Any idea how to fix the problem?

tripleee
  • 175,061
  • 34
  • 275
  • 318
amkleist
  • 181
  • 1
  • 2
  • 11
  • 2
    Do you have to use Python 2.7? This error would not happen in 3.x. – DYZ Feb 07 '20 at 07:48
  • Yes - I have to use Python 2.7. Declaring the string as unicode does not solve my problem - as I also wrote in my post. – amkleist Feb 07 '20 at 07:59
  • 1
    I suggest you to read this article so when you find this kind of bug you understand how to solve it: https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ – off99555 Feb 07 '20 at 08:20

2 Answers2

1

You should encode your string (updated encoding type):

os.system(u'cscript myVbsScript.vbs -i "This is a test" "1000" "æøå"'.encode("iso-8859-1"))
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 2
    UTF-8 may not be the correct encoding though, since this is Windows. MBCS possibly, or something else altogether. – AKX Feb 07 '20 at 08:05
  • That gives me the error: `SyntaxError: Non-ASCII character '\xe6' in file D:\sandbox\keybordEmulator\test.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details` – amkleist Feb 07 '20 at 08:07
  • 2
    Because apparently you removed the `# -*-coding:utf-8 -*-` -- the error message tells you this. This is another thing which Python 3 handles by simply declaring that UTF-8 is the default encoding for scripts. (Again, we have to assume that your text is *actually* UTF-8; there is no evidence to the contrary, but sometimes people blindly apply these things without understanding what they mean.) – tripleee Feb 07 '20 at 08:07
  • 1
    @AKX you were right! I had to use the the following: `os.system(u'cscript myVbsScript.vbs -i "This is a test" "1000" "æøå"'.encode("iso-8859-1"))` – amkleist Feb 07 '20 at 09:18
0

The solution:

# -*-coding:utf-8 -*-
import os
os.system(u'cscript myVbsScript.vbs -i "This is a test" "1000" "æøå"'.encode('iso-8859-1')

The .vbs-script couldn't read the utf-8 encode.

amkleist
  • 181
  • 1
  • 2
  • 11