您的位置:首页 > 编程语言 > Python开发

python中的getopt怎么理解

2018-01-22 16:50 337 查看
在看crfascnn代码的demo时看到这个语句,上网查了一下,可以参考这个博文:

http://blog.csdn.net/chengxuyuanyonghu/article/details/42556885

demo中相关的代码以及我的解释如下:

try:
opts, args = getopt.getopt(argv, 'hi:o:g:', ["ifile=", "ofile=", "gpu="]) # argy为运行程序时自己添加的参数,如运行程序:python crfasrnn_demo.py -i my_pic.jpg -o my_out.png -g -1 即可指定自己的图片为my_pic.jpg,输出my_out.png
except getopt.GetoptError:
print("crfasrnn_demo.py -i <input_file> -o <output_file> -g <gpu_device>")
sys.exit(2)

for opt, arg in opts:
if opt == '-h':
print("crfasrnn_demo.py -i <inputfile> -o <outputfile> -g <gpu_device>")
sys.exit()
elif opt in ("-i", "ifile"): #若指定了ifile的参数,更新input_file
input_file = arg
elif opt in ("-o", "ofile"): #若指定了ofile的参数,更新output_file
output_file = arg
elif opt in ("-g", "gpudevice"):
gpu_device = int(arg)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: