In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people have no idea about how to use the sys.argv [] method in python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Sys.argv [] is used to get command-line arguments, and sys.argv [0] represents the file path of the code itself, so start with the second, sys.argv [1].
Note: parameters are separated by spaces
Create a file called sysargv.py with the following contents:
Import sys
Print ('the first argv:', sys.argv [0],'\ n') # shows the first parameter
Print ('the second argv:', sys.argv [1],'\ n') # shows the second parameter
Print ('the third argv:', sys.argv [2],'\ n') # shows the third parameter, and so on
The results of the execution are as follows:.
E:\ lib > learnsysargv.py yang.txt yangqilong
The first argv: e:\ lib\ learnsysargv.py-- the path to the pyython file
The second argv: yang.txt-- Parameter 1
The third argv: yangqilong-- Parameter 2
I revised an example in the official textbook.
Import sys
Print ('the first argv:', sys.argv [0])
Print ('the second argv:', sys.argv [1])
# print ('the third argv:', sys.argv [2])
If len (sys.argv)
< 2: print ('No action specified.') sys.exit() 解释一下下面IF语句的语法:判断参数1是否是以'--'开头 if sys.argv[1].startswith('--'): ption = sys.argv[1][2:]//把参数1从第三个字符开始的字符串赋值给 option(去掉'--'字符),直到参数1后面的空格。 # fetch sys.argv[1] but without the first two characters if ption == 'v': print ('Version 1.2') elif option == 'h': print ('This program prints files to the standard output.\nAny number of files can be specified.\n\ Options include:\n\ --v : Prints the version number\n\ --h : Display this help') 程序输出结果: E:\lib>Sysargv.py-- h
The first argv: e:\ lib\ sysargv.py
The second argv:-h
This program prints files to the standard output.
Any number of files can be specified.
Options include:
-v: Prints the version number
-h: Display this help
E:\ lib > sysargv.py-- v
The first argv: e:\ lib\ sysargv.py
The second argv:-- v
Version 1.2
After reading the above, have you mastered how to use the sys.argv [] method in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.