How do you protect yourself from missing comma in vertical string list in python?

You can have commas at the end of a line after whitespace, like this:

subprocess.check_output( [
   'application'           ,
   '-first-flag'           ,
   '-second-flag'          ,
   '-some-additional-flag' ,
] )

Doing it that way looks a little worse, but it is easy to spot if you have missed any arguments.


You could wrap each string in parens:

subprocess.check_output( [
  ('application'),
  ('-first-flag'),
  ('-second-flag'),
  ('-some-additional-flag'),
] )

And btw, Python is fine with a trailing comma, so just always use a comma at the end of the line, that should also reduce errors.


maybe for this particular case:

arglist = 'application -first-flag -second-flag -some-additional-flag'
arglist = arglist.split()
subprocess.check_output(arglist)

Or if you find yourself writing many unique lists like this make a macro that concatenates lines into a list form, so you avoid manually putting the comma.

Tags:

Python