Passing directory to python script as command line argument

匿名 (未验证) 提交于 2019-12-03 01:02:01

问题:

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.

回答1:

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') 


回答2:

check the sys.argv here

import sys  print sys.argv[1]# this gives directory name 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!