Skip to content
Advertisement

How to override output filepath and write it along with output files at a desired directory through argparse

I have the following example code and I use argparse to call and execute it.

JavaScript

Below is the main.py script where I call the above code via argparse

JavaScript

Since I’m working on a python pipeline, I would like to override the output filepath and give it via CLI, i.e., as an arseparse argument. As I’ve said in the beginning, the test1.py is an example code, in reality it will have several lines of code and I dont want the user of main.py to go inside and modify it manually. Thus, my question is, can the output filepath be overridden via argparse ? such that I can collect the outputs from test1.py and write the .txt and .png file at a desired directory and desired filenames. For example, the command line should look like this:

JavaScript

Thanks

Advertisement

Answer

You can do it adding this line to main.py:

JavaScript

After that pass output directory to test function:

JavaScript

Change the test function to accept the output directory and change the path in plt.savefig

JavaScript
Advertisement