0

I wrote a script that makes some calculation and it receives input parameters using argparse.

One of these parameters is a list of file paths.

Looking around I found this other question and I modify my code adding:

import argparse

parser = argparse.ArgumentParser(description='Engine')
parser.add_argument('--outputFile', required=True, help='Output file path [str]')
parser.add_argument('--filesList', nargs='+', required=True, help='List of the input files [list of str]')
args = parser.parse_args()

outputFileName = args.outputFile
inputFilesName = args.filesList

In this way I can pass all the input parameters even the list of the files typing each one separated by a space, like:

--outputFile output.nc
--filesList data/file1.nc data/file2.nc

Now I created the GUI for my script and I call the script using subprocess:

command = [sys.executable,
           'D:/Projects/Calculation/src/modules/Engine.py',
           '--outputFile', self.outputFileName,
           '--filesList', filesPathList]

 p = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=0)

where filesPathList is the list of files path: ['data/file1.nc', 'data/file2.nc']

The problem is if I run the software in this way, what the engine understand is a list of 1 element: ['data/file1.ncdata/file2.nc'], essentially arcparse fuses together all the paths.

Is there any option I can add to add_argument to fix it?

I could change the way to parse the input parameters, but I would like to know if there is a way using argparse.

cicciodevoto
  • 305
  • 4
  • 19
  • Try `... '--filesList', *filesPathList]` so the names are placed individually in the `command` list. Look at that list, and compare it the `sys.argv` list you get from entering the command in the shell. – hpaulj May 30 '19 at 20:24
  • @hpaulj thanks for the suggestion, but I am using Python 2.7 and it doesn't support the starred expression – cicciodevoto May 31 '19 at 08:27
  • 1
    Try addi g that liat to `command` wuth `extend` – hpaulj May 31 '19 at 10:53
  • @hpaulj Thanks it woks in that way. Now the code is: `command = [..., '--filesList'] command.extend(filesPathList)`, I don't think the code is really clear in this way, but it works and I really appreciate your help. – cicciodevoto May 31 '19 at 14:11

0 Answers0