-1

I have a few sub-directories that live in the same folder as my python script. I am trying to run the script inside one of the sub-directories.

example:

$ script.py subdirectory

Is there a way to pass in the sub directory at command line

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Can you explain a bit more what exactly you're doing? – surya Jun 14 '21 at 10:39
  • You have two sub problems. 1. How to pass command line arguments, 2. How to change directory in the program. Search for these separately, you would be able to put it together. – Sayandip Dutta Jun 14 '21 at 10:40
  • You can use ```python``` and then space and the path to your file. It can be done with the file too –  Jun 14 '21 at 10:41
  • Possible duplicate of https://stackoverflow.com/questions/19587118/iterating-through-directories-with-python – Omkar Kadam Jun 14 '21 at 10:41

1 Answers1

1
import argparse

parser = argparse.ArgumentParser()

parser.add_argument("-subdir", "--subdirectory", help="enter subdirectory name")

params = parser.parse_args()

subdir = params.subdirectory

## and continue your code using subdir as the used subdirectory name

So when you want to run:

script.py -subdir SUBDIRECTORY_NAME_HERE