其他

__name__

判断是否是命令行运行:if __name__ == '__main__'

获取命令行参数:sys.argv

python3 demo.py 会输出 Hello, world!

python3 demo.py BestLove 会输出 Hello, BestLove!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'BestLove'

import sys

def test():
    args = sys.argv
    if len(args) == 1:
        print('Hello, world!')
    elif len(args) == 2:
        print('Hello, %s!' % args[1])
    else:
        print('Too many arguments!')


if __name__ == '__main__':
    test()

results matching ""

    No results matching ""