I want to pass 4 arguments to my python script and one of them has to be a list. Even if it just contains 1 element.
The order of the arguments does not matter.
import sys
print sys.argv
one = sys.argv[1]
two = sys.argv[2]
three = sys.argv[3]
four = sys.argv[4]
print "one: " + one
print "two: " + two
print "three: "+ three
print "four: " + four
This is how I am calling it.
python myScript.py name file setting ['listItem1']
Where the fourth item is the list with one element. However when I print it I see
four: [listItem1]
I would like to see
four: ['listItem1']
as PersonArtPhoto pointed out, you need to escape the qutoation marks to prevent the shell from using them itself.
What you’re going to wind up with in your program is a string that resembles a list, which isn’t what you want
shows
this
'f'is coming from indexing"['first','second']"because it is just a string of charactersone way to make python interpret this is
evalbut it is highly discouraged as it is extremely insecure.evalinterprets a string as code, so it shouldn’t be used on user input because the user can enter anything and python will execute it.shows
instead I suggest a safer approach. Use a command line flag to signal that you are sending the list of arguments
shows
if you know that there will always be three args before your list, you can simply use a slice
shows