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,
- import sys,os
- os.system(sys.argv[1])
- import sys,os
- 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.
- Import sys
- Def readfile (filename): # Read the file content from the file
- ''' Print a file to the standard output .'''
- F = file (filename)
- While True:
- Line = f. readline ()
- If len (line) = 0:
- Break
- Print line, # notice comma output the content of each line separately
- If. close ()
- # Script starts from here
- If len (sys. argv) <2:
- Print 'no action specified .'
- Sys. exit ()
- 15if sys. argv [1]. startswith ('--'):
- Option = sys. argv [1] [2:]
- # Fetch sys. argv [1] but without the first two characters
- If option = 'version': # When the command line parameter is -- version, the version number is displayed.
- Print 'version 1.2'
- Elif option = 'help': # The help content is displayed when the command line parameter is -- help.
- Print '''''\
- This program prints files to the standard output.
- Any number of files can be specified.
- Options include:
- -- Version: Prints the version number
- -- Help: Display this help '''
- Else:
- Print 'unknown option .'
- Sys. exit ()
- Else:
- For filename in sys. argv [1:]: # When the parameter is a file name, input readfile to read its content.
- Readfile (filename)
- Import sys
- Def readfile (filename): # Read the file content from the file
- '''Print a file to the standard output .'''
- F = file (filename)
- While True:
- Line = f. readline ()
- If len (line) = 0:
- Break
- Print line, # notice comma output the content of each line separately
- F. close ()
- # Script starts from here
- If len (sys. argv) <2:
- Print 'no action specified .'
- Sys. exit ()
- If sys. argv [1]. startswith ('--'):
- Option = sys. argv [1] [2:]
- # Fetch sys. argv [1] but without the first two characters
- If option = 'version': # When the command line parameter is -- version, the version number is displayed.
- Print 'version 1.2'
- Elif option = 'help': # The help content is displayed when the command line parameter is -- help.
- Print '''\
- This program prints files to the standard output.
- Any number of files can be specified.
- Options include:
- -- Version: Prints the version number
- -- Help: Display this help '''
- Else:
- Print 'unknown option .'
- Sys. exit ()
- Else:
- For filename in sys. argv [1:]: # When the parameter is a file name, input readfile to read its content.
- 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.