# Python making call list for C++ executable

I have a text file which contains configuration values that are intended to be arguments to a C++ executable (to be read in as a vector in C++). I am using a Python script to read in the text file and then call the C++ executable as a subprocess. The reason I'm doing it via Python is because I do not want to modify the C++ executable for unrelated reasons.

Here is my code snippet to achieve this. I was wondering if there's a better way to do this.

call_list = [executable, "--directory", directory, "--filePrefix", file_prefix, "--configList"]

config_file = directory + file_prefix + ".txt"
config_list = [line.strip() for line in open(config_file)]

config_list_split = []
for config in config_list:
config_list_split.extend(config.split())

call_list.extend(config_list_split)

subprocess.check_call(call_list)


Putting an open() function inside a list comprehension seems a bit iffy to me, especially since you don't seem to close it anywhere.

List comprehensions are generally better than for loops.

The config_file name is a bit misleading, as it's the filename, not the file object.

So alternative code would be:

config_file_name = directory + file_prefix + ".txt"
with  open(config_file_name) as config_file:
config_list_split = [config_item for config in config_file for config_item in config.split()]


This is less transparent, but performance-wise I would expect it to be better than a for-loop.

• Could you elaborate "iffy"? What things could go wrong with that design? – simplename Apr 11 '18 at 15:07