sys.argv的用法

2017-10-04  本文已影响0人  modi1492

sys.argv用于取得执行python程序时的参数

通过一个示例说明

➜  sys_argv tree.

├── __init__.py

├── argv_01.py

├── argv_02.py


# argv_01.py

importsys

fromsys_argv.argv_02importargv_02_func_01

print('this is argv_01')

print(sys.argv)

argv_02_func_01()


# argv_02.py

importsys

defargv_02_func_01():

      print('this is argv02')

      print('this is argv02')

      print('this is argv02')

      print(sys.argv)

      print('this is argv02')

      print('this is argv02')


运行argv_01后的输出结果为:

this is argv_01

['/Users/mac/Django/python高级语法/sys_argv/argv_01.py']

this is argv02

this is argv02

this is argv02

['/Users/mac/Django/python高级语法/sys_argv/argv_01.py']

this is argv02

this is argv02

可见:

在文件2中打印sys.argv,也是打印文件1的文件名。

上一篇下一篇

猜你喜欢

热点阅读