user2294884
user2294884

Reputation: 1

Pass complex object to a python script in command line

I am beginner in Python programing, I want to pass a complex object (Python dictionary or class object) as an argument to a python script using command line (cmd). I see that sys.argv gets only string parameters.

Here is an example of what I want:

class point(object):
     def __init__(self,x,y):
         self.__x=x
         self.__y=y
p=point(4,8)
import os
os.system('""xxxxxx.exe" "-s" "../create_scenario.py" "'+ p +'""')

The xxxxxx.exe is a program which accept to run a python script with the -s option. Thanks for all!!!

Upvotes: 0

Views: 5382

Answers (2)

Odomontois
Odomontois

Reputation: 16308

You could try serialize\deserialize to\from a string using a pickle module. Look for dumps and loads

Upvotes: 1

aldeb
aldeb

Reputation: 6828

You could use eval:

my_dict = eval(sys.argv[1])
print(my_dict)

Comand prompt:

$ my_script.py {1:2}
$ {1: 2}

But what you want to do is not recommended (see this post). You should avoid this and store your data in a file instead (using JSON for example).

Upvotes: 0

Related Questions