Home > Mobile >  os.system argument from config file
os.system argument from config file

Time:10-30

I made this activity and it works. I need to have config file with USB/VID/PID.

def resetactivity():
        os.system(r'"devcon.exe restart "*USB\VID_04E8&PID_3321*"')

I try to do this with config parser. I made config.txt:

[My Section]
usbdev = r'"devcon.exe restart "*USB\VID_04E8&PID_3321*"'

I read my config file in Python:

config = configparser.ConfigParser()
config.read('config.txt')
usbdev = config.get('My Section', 'usbdev')

And when I am trying to use this in os.system command like this:

def resetactivity():
        os.system(usbdev)

I get this result:

The filename, directory name, or volume label syntax is incorrect. 'PID_3321*"'' is not recognized as an internal or external command, operable program or batch file.

CodePudding user response:

Try this code

import configparser
import os

def resetactivity():
    config = configparser.ConfigParser()
    config.read('config.txt')
    usbdev = config.get('My Section', 'usbdev')
    print(usbdev)
    os.system(usbdev)

if __name__ == "__main__":
    resetactivity()

With config.txt formatted as

[My Section] 
usbdev = devcon.exe restart "USB\VID_04E8&PID_3321"
  • Related