Python sys.argv[] Usage

Source: Internet
Author: User

Sys.argv[] is used to get command line arguments, Sys.argv[0] represents the code itself file path, so the parameters starting from 1, the following two examples illustrate:

1. A simple example of using sys.argv[]:
The following is the sample1.py file:

[Python]View PlainCopy
    1. Import Sys,os
    2. Print SYS.ARGV
    3. Os.system (sys.argv[1])


Os.system ("Bash command") runs a shell command that directly displays and simulates a person's operation,

This example Os.system receive command line arguments, run parameter directives, cmd command line with parameters run Python sample1.py Notepad, will open the Notepad program.
2, this example is a concise Python tutorial, understand it after you understand sys.argv[].

The following is the sample.py file:

[Python]View PlainCopy 
  1. #!/usr/bin/env python
  2. #_ *_ Coding:utf-8 _*_
  3. Import Sys
  4. def readfile (filename): #定义readfile函数 to read the contents of a file from a file
  5. "" " Print a file to the standardoutput. ' "'
  6. f = file (filename)
  7. while True:
  8. line = F.readline ()
  9. If Len (line) = = 0:
  10. Break
  11. print line, # notice comma output each row of content separately
  12. F.close ()
  13. # Script starts from here
  14. Print SYS.ARGV
  15. If Len (SYS.ARGV) < 2:
  16. print ' No action specified. '
  17. Sys.exit ()
  18. If sys.argv[1].startswith ('--'):
  19. option = sys.argv[1][2:]
  20. # Fetch sys.argv[1] But without the first and the characters
  21. if option = = ' version ': #当命令行参数为--version, display the revision number
  22. print ' Version 1.2 '
  23. elif option = = ' help ': when #当命令行参数为--help, the relevant helper content is displayed
  24. print " "
  25. Prints files to the standard output.
  26. Any number of the files can be specified.
  27. Options include:
  28. --version:prints the version number
  29. --help:display this " "
  30. Else:
  31. print ' Unknown option. '
  32. Sys.exit ()
  33. Else
  34. For filename in sys.argv[1:]: #当参数为文件名时, incoming ReadFile, read out its contents
  35. ReadFile (filename)



In the same directory as sample.py, create a new 3 Notepad file Test.txt,test1.txt,test2.txt, as follows:

Verify the sample.py as follows:
C:\users\91135\desktop>python sample.py
[' sample.py ']

No action specified.

C:\users\91135\desktop>python sample.py--help

[' sample.py ', '--help ']

Prints files to the standard output.
Any number of the files can be specified.
Options include:
--version:prints the version number

--help:display this Help

C:\users\91135\desktop>python sample.py--version
[' sample.py ', '--version ']

Version 1.2

C:\users\91135\desktop>python sample.py--ok
[' sample.py ', '--ok ']

Unknown option.

C:\users\91135\desktop>python sample.py Test.txt
[' sample.py ', ' test.txt ']

Hello python!

C:\users\91135\desktop>python sample.py test.txt test1.txt test2.txt
[' sample.py ', ' test.txt ', ' test1.txt ', ' test2.txt ']
Hello python!
Hello world!

Hello wahaha!

goodbye!

C:\users\91135\desktop>

Python sys.argv[] Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.