Lain
Lain

Reputation: 2216

os.system command in python reading improper for wifi name

Not 100% what to title this post. It is a pretty simple question though

First I ran:

os.system("netsh interface show interface")

figured out my wifi was called "Wi-fi 2"

Then I wanted to make 2 simple functions for turning it on and off

import os 

def enable():
    os.system("netsh interface set interface 'Wi-Fi 2' enabled")
def disable():
    os.system("netsh interface set interface 'Wi-Fi 2' disabled")

Also tried doing it a few other ways like

interface "+"Wi-Fi 2"+" disabled")

When I call disable though it gives me this error:

2 is not an acceptable value for admin.
The parameter is incorrect.

it is reading the 2 as a seperate parameter (have confirmed by trying Wi-Fi 3 and it says 3 is not an acceptable value).

Am I not doing this string proper somehow? No idea why this is happening, I would rather not have to rename the Wifi since thats just a poor work around instead of understanding the problem and fixing it.

Thanks

Upvotes: 0

Views: 313

Answers (1)

CristiFati
CristiFati

Reputation: 41116

In Win cmd shell (although this is not restricted to Win), SPACE (ASCII 0x20(32)) is a separator (or: one of them), meaning that when parsing a sequence of characters when encountering a SPACE, what's before will be treated as one token, while what comes after it, will be treated as another token (and so on).

The reverse (if you will) is the dblquote (" or ASCII 0x22(34)): what's contained between 2 such characters will be treated as one token, regardless if it contains SPACEs.

This applies to:

  • File paths (that's why dir c:\Program Files fails, while dir "c:\Program Files" works - and the latter should be used)
  • Command arguments (same thing here)

This is what needs to be done from OS's perspective, we still need to handle the Python string (that wraps all this). It can be done in 2 ways as explained here: [Python]: String literals:

  • Escaping " by a bkslash (\ or ASCII 0x5C(92)), since the string is also enclosed by dblquotes: os.system("netsh interface set interface \"Wi-Fi 2\" enabled")
  • Using the single quote (' or ASCII 0x27(39)) to enclose the string: os.system('netsh interface set interface "Wi-Fi 2" enabled')

Upvotes: 1

Related Questions