Python sys. argv usage example, pythonsys. argv

Source: Internet
Author: User

Python sys. argv usage example, pythonsys. argv

The sys. argv variable is a string list. In particular, sys. argv contains a list of command line parameters, that is, the parameters passed to your program using the command line.

Here, when we execute python using_sys.py we are arguments, we use the python command to run the using_sys.py module. The following content is passed to the program as a parameter. Python stores it in the sys. argv variable for us. Remember, the script name is always the first parameter in the sys. argv list. So here, 'using _ sys. py 'is sys. argv [0] And 'We' are sys. argv [1] and 'are' are sys. argv [2] and 'arguments' are sys. argv [3]. Note that Python starts counting from 0, not from 1.

Sys. argv [] is used to obtain command line parameters, sys. argv [0] indicates the path of the code file. For example, enter "python test. py-help ", then sys. argv [0] indicates "test. py ".

Sys. startswith () is used to determine the starting point of an object. For example, if you enter 'abc'. startswith ('AB') in the python command line, True is returned.
For the following examples:
Copy codeThe Code is as follows:
#! /Usr/local/bin/env python
Import sys
Def readfile (filename ):
'''Print a file to the standard output .'''
F = file (filename)
While True:
Line = f. readline ()
If len (line) = 0:
Break
Print line,
F. close ()
Print "sys. argv [0] ---------", sys. argv [0]
Print "sys. argv [1] ---------", sys. argv [1]
Print "sys. argv [2] ---------", sys. argv [2]
# 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 ':
Print 'version 1.2'
Elif option = '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:]:
Readfile (filename)

Execution result: # python test. py -- version help
Copy codeThe Code is as follows:
Sys. argv [0] --------- test. py
Sys. argv [1] --------- -- version
Sys. argv [2] --------- help
Version 1.2:

Note: sys. argv [1] [2:] indicates to intercept the second parameter from the third character to the end. In this example, the result is: version.

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.