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的文件名。