4

I am trying to pass a particular directory to a python script and later use that directory in the script . the Directory can be located anywhere. for example, the script should run on the command line as

script.py directory_name

So far I looked but nothing as such.

geekyjazzy
  • 95
  • 1
  • 2
  • 7

2 Answers2

5

You can do it as:

directory_name=sys.argv[n]

It is always good to catch the error, if directory name is not provided by the user.

import sys

...
...

try:
    directory_name=sys.argv[1]
    print(directory_name)
except:
    print('Please pass directory_name')
Scorpion_God
  • 1,499
  • 10
  • 15
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
1

check the sys.argv here

import sys

print sys.argv[1]# this gives directory name
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46