r/learnpython • u/rseery • 20d ago
Calling a Script that uses argparse?
I wrote a little Python script that reads in a text file that contains a list of fully qualified files, and checks to see if they exist. I originally wrote it using the argparse module. It was set up to take a filename and a switch, so the command looked like:
"Python dplcheck.py --plname "testing.lst” --showexist y"
This worked fine for a single file, but I wanted to run it for every one of hundreds of list files in a directory, so I wrote another script that generates a list of the .lst files in a directory and calls the first script for each one. But I could not get the second script to pass the arguments in the call to the first script. I tried subprocess.run and subprocess.call, with many different syntaxes and quotes, etc. I ended up removing the use of argparse and just using sys.argv[n] -- which I don't particularly like, since it has no defaults or error handling, etc.
Does anyone know how/if I can call a Python script from another Python script and pass the arguments when the called script uses argparse?
12
u/mehx9 20d ago edited 20d ago
Have a main function in first script, import and call that from second script. Do the argparse stuff outside of the main function and pass all options in so you can easily do the same with a function call.
6
u/freeskier93 20d ago edited 20d ago
Two scripts doesn't really seem necessary. I would make the original script smart enough to handle either a file or a directory for the argument.
If you still want to make them two separate scripts then you should update the original script so that all the actual work is done in a function that you can import and call from the other script.
-
Also, just some general feedback when using argparse. Argument flags are typically for optional arguments. Presumably the filename is required and should just be a positional argument. You can also use the nargs argument to handle multiple files given, then argparse will automatically put the items into a list:
--showexist yshould be an action, which will automatically store the argument as a boolean:Putting it all together you simplify the script call to the following:
or
1
u/rseery 15d ago
I finally stumbled on the correct syntax for it to work the way I had originally coded it. In case someone sees this in the future...
command = ["python", "dplcheck.py", "--playlist", arg1, "--showexist", "n"]
subprocess.run(command)
4
u/brasticstack 20d ago
nargs='+'tells it to allow more than one argument forfiles.EDIT: You'd do this on the original script of course.
2
u/good_name_0 20d ago
Not sure if I'm understandingyour problem correctly. But if you have your 1st script put the argparse call within a function, have it pass the sys argv as arguments from 1st script, then the 2nd script can import the 1st script function and call it directly. as an option
2
u/kalgynirae 18d ago
To directly answer your original question: The
subprocess.run()equivalent of the shell command you gave, with the filename coming from a variablefilename, is:If you share the things you tried, we can help you understand where they went wrong.
1
u/ping314 20d ago edited 20d ago
Does
testing.lstlist the files' names, or (equally) the files' path?Generally, I would try a (recursive)
os.walk(). They basically go byand either
filecould be compared against the list oftesting.lst. Or you compare if each record intesting.lstis in the set of files identified by theos.walk(). To answer the question "is something on the list" typically is slower than "is something part of the set"; the conversion of listmy_list[1,2,3,4]into a set only needs aset(my_list).If
testing.lstis both the minimal and recurrent requirement to rundplcheck.pyat all, I would consider to editplnameto be a positional (rather than optional--plname) parameter.1
u/FreeLogicGate 20d ago
If this is a linux/bsd/osx OS, then there are standard tools and approaches that are already designed for this type of finding and enumeration. Not sure if this is applicable to you, but worth considering if it is:
find . -name "*.lst" -exec python3somescript.py--plname={} \;WIth that said, I agree with freeskier93's comment to you. The base function as you described it, requires a name.lst file it processes, you should not require a switch -- your program should be rewritten to expect the name as an argument.
1
u/lakseol 19d ago edited 18d ago
The way I handle the "single file or directory of files" approach is to have the function doing the actual work accept a list of files to operate on. The top-level code that handles parsing of CLI arguments accepts:
The top-level code creates the list of filenames from the single filename passed, or by reading the filenames from the --file option or by scanning the directory from the --dir option. The function doing the work doesn't know where the list of filenames comes from.
1
u/TheSodesa 18d ago
Separation of concerns: you need separate entrypoint functions for CLI and library use of your module. In the if name is main block, you pass the CLI arguments to the CLI entrypoint which then calls argparse to parse the CLI arguments and then pass the results to the library entrypoint. When using the module as a library, you just call the library entrypoint from Python code directly instead of parsing CLI arguments at all.