How do I pass variable when using Python subprocess module

后端 未结 2 1688
鱼传尺愫
鱼传尺愫 2020-12-12 03:58

I\'m trying to use python Subprocess module to enable/disable Ethernet connection from python code. Below is my code in which the first step is looking for the available \"E

相关标签:
2条回答
  • 2020-12-12 04:59

    Use string formatting, interpolation, or concatenation:

    x = 'Ethernet10'
    subprocess.call('netsh interface set interface ' + x + ' ENABLED')
    
    0 讨论(0)
  • 2020-12-12 05:02

    subprocess.call takes a list as an argument:

    subprocess.call(['netsh', 'interface', 'set', 'interface', x, 'ENABLED'])
    

    You could instead pass shell=True and your string would work, but it is a security risk, since an user could for example call another command by using $(command_here)

    If you still want to use a string, you could use shlex.split.

    0 讨论(0)
提交回复
热议问题