将目录作为命令行参数传递给python脚本 [英] Passing directory to python script as command line argument
本文介绍了将目录作为命令行参数传递给python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图传递一个特定的目录到一个python脚本,以后在脚本中使用该目录。目录可以位于任何地方。
例如,脚本应该在命令行上运行
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.
推荐答案
您可以这样做:
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')
这篇关于将目录作为命令行参数传递给python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文