Nikita Belooussov
Nikita Belooussov

Reputation: 606

Python subprocess using JSON dictionary as argument

I am trying to pass a JSON dictionary as an argument through the terminal using a subprocess. The dictionary keeps ending up different from when I pass it manually through the terminal. I am using this code for the subprocess:

subprocess.call("python ../power_supply_gui/PowerSupplyControl.py "+ "{\"CHANNEL\":\"d\",\"VOLT\":\"1\",\"CURRENTLIMIT\":\"1\",\"ENABLE\":\"1\"}",shell=True)

I should be getting this when I read it in the other program:

{"CHANNEL":"d","VOLT":"1","CURRENTLIMIT":"1","ENABLE":"1"}

but instead, I am getting this:

{CHANNEL:d,VOLT:1,CURRENTLIMIT:1,ENABLE:1}

This is how the program that is called is reading the argument and outputting it:

print sys.argv[1]
print type(sys.argv[1])
commandDictionary=json.loads(sys.argv[1]) 

Upvotes: 0

Views: 3804

Answers (1)

Jon Deaton
Jon Deaton

Reputation: 4379

The conversion of the dictionary to a json string puts a space after the colon which means that before the space and after the space are split up into different arguments. If you want this to work try removing the spaces with str(dictionary).replace(" ","") as the argument.

Upvotes: 2

Related Questions