python sys.argv 默认值

xiaoxiao2021-02-28  14

sys.argv其实就是个list,如果为空,可以argv=[] 如果为None: args = sys.argv[1:] args.append("pascal") args.append("pascal_path") 如果为None: #!/usr/bin/python # -*- coding: utf-8 -*- import sys def main(argv): if argv == None: argv=[] argv.append("asdfsdf") print(argv) print('world~!') else: argv.append("asdfsdf") print(argv) print('hello') if __name__ == '__main__': main(None) 如果不为None: #!/usr/bin/python # -*- coding: utf-8 -*- import sys def main(argv): if argv == None: print('world~!') else: argv.append("asdfsdf") print(argv) print('hello') if __name__ == '__main__': main(sys.argv) sys.argv其实就是个list,如果为空,可以argv=[] 如果为None: args = sys.argv[1:] args.append("pascal") args.append("pascal_path") 如果为None: #!/usr/bin/python # -*- coding: utf-8 -*- import sys def main(argv): if argv == None: argv=[] argv.append("asdfsdf") print(argv) print('world~!') else: argv.append("asdfsdf") print(argv) print('hello') if __name__ == '__main__': main(None) 如果不为None: #!/usr/bin/python # -*- coding: utf-8 -*- import sys def main(argv): if argv == None: print('world~!') else: argv.append("asdfsdf") print(argv) print('hello') if __name__ == '__main__': main(sys.argv) from optparse import OptionParser usage = "usage: %prog [options] arg1 arg2" parser = OptionParser(usage=usage) # 创建OptionParser对象 parser.add_option("-v", "--verbose", # add_option增加选项 action="store_true", dest="verbose", default=True, help="make lots of noise [default]") parser.add_option("-q", "--quiet", action="store_false", dest="verbose", help="be vewwy quiet (I'm hunting wabbits)") parser.add_option("-f", "--filename", metavar="FILE", help="write output to FILE"), parser.add_option("-m", "--mode", default="intermediate", help="interaction mode: novice, intermediate, " "or expert [default:
转载请注明原文地址: https://www.6miu.com/read-1649979.html

最新回复(0)