Correct understanding of how to use Python sys. arg

Source: Internet
Author: User

In the Python programming language, there are many special application methods that deserve our taste. Here we will give you a detailed analysis of how to use Python sys. arg, hoping to provide some help for your friends to facilitate their use in applications.

Python sys. arg is used to obtain the command line parameters. sys. argv [0] indicates the file path of the Code. Therefore, the parameter starts from 1. The following two examples illustrate:

1. Use a simple instance of sys. argv,

 
 
  1. import sys,os   
  2. os.system(sys.argv[1])   
  3. import sys,os  
  4. os.system(sys.argv[1]) 

In this example, OS. system receives command line parameters, runs parameter commands, and saves them as sample1.py. The command line runs sample1.py notepad with parameters, and opens the notepad program.

2. This example is a concise Python tutorial. After understanding it, you will understand how to use Python sys. arg.

 
 
  1. Import sys
  2. Def readfile (filename): # Read the file content from the file
  3. ''' Print a file to the standard output .'''
  4. F = file (filename)
  5. While True:
  6. Line = f. readline ()
  7. If len (line) = 0:
  8. Break
  9. Print line, # notice comma output the content of each line separately
  10. If. close ()
  11. # Script starts from here
  12. If len (sys. argv) <2:
  13. Print 'no action specified .'
  14. Sys. exit ()
  15. 15if sys. argv [1]. startswith ('--'):
  16. Option = sys. argv [1] [2:]
  17. # Fetch sys. argv [1] but without the first two characters
  18. If option = 'version': # When the command line parameter is -- version, the version number is displayed.
  19. Print 'version 1.2'
  20. Elif option = 'help': # The help content is displayed when the command line parameter is -- help.
  21. Print '''''\
  22. This program prints files to the standard output.
  23. Any number of files can be specified.
  24. Options include:
  25. -- Version: Prints the version number
  26. -- Help: Display this help '''
  27. Else:
  28. Print 'unknown option .'
  29. Sys. exit ()
  30. Else:
  31. For filename in sys. argv [1:]: # When the parameter is a file name, input readfile to read its content.
  32. Readfile (filename)
  33. Import sys
  34. Def readfile (filename): # Read the file content from the file
  35. '''Print a file to the standard output .'''
  36. F = file (filename)
  37. While True:
  38. Line = f. readline ()
  39. If len (line) = 0:
  40. Break
  41. Print line, # notice comma output the content of each line separately
  42. F. close ()
  43. # Script starts from here
  44. If len (sys. argv) <2:
  45. Print 'no action specified .'
  46. Sys. exit ()
  47. If sys. argv [1]. startswith ('--'):
  48. Option = sys. argv [1] [2:]
  49. # Fetch sys. argv [1] but without the first two characters
  50. If option = 'version': # When the command line parameter is -- version, the version number is displayed.
  51. Print 'version 1.2'
  52. Elif option = 'help': # The help content is displayed when the command line parameter is -- help.
  53. Print '''\
  54. This program prints files to the standard output.
  55. Any number of files can be specified.
  56. Options include:
  57. -- Version: Prints the version number
  58. -- Help: Display this help '''
  59. Else:
  60. Print 'unknown option .'
  61. Sys. exit ()
  62. Else:
  63. For filename in sys. argv [1:]: # When the parameter is a file name, input readfile to read its content.
  64. Readfile (filename)

Save the program as sample. py. Let's verify it:

  • Python data conversion code in-depth analysis
  • In-depth exploration of the magic of the Python Sequence
  • Python for in code Operation Method
  • Main functions of the Python Thread class
  • Analysis on the practical application of Python two-dimensional array

<! -- [If! SupportLists] --> 1) <! -- [Endif] --> run the command line with parameters: sample. py-version. The output result is: version 1.2.

<! -- [If! SupportLists] --> 2) <! -- [Endif] --> run the command line with parameters: sample. py-help. The output result is: This program prints files ......

<! -- [If! SupportLists] --> 3) <! -- [Endif] --> in the same directory as sample.py, the.txt notepad file is created with the content test argv. Run the command line with parameters: sample. py a.txt, the output result is a.txt file content: test argv. Here you can also include several more parameters. The program will output the parameter file content successively. The above describes how to use Python sys. arg.

Related Article

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.