Python SYS.ARGV Usage Examples

Source: Internet
Author: User
Tags readfile

This article mainly introduces Python sys.argv usages, sys.argv[] is used to get command-line arguments, Sys.argv[0] represents the file path of the code itself, others are used to indicate getting input parameters, the friends you need can refer to the

The SYS.ARGV variable is a list of strings. Specifically, SYS.ARGV contains a list of command-line arguments, that is, the arguments that are passed to your program using the command line.

Here, when we execute the Python using_sys.py we are arguments, we run the using_sys.py module using the Python command, followed by the contents being passed as arguments to the program. Python stores it in SYS.ARGV variables for us. Remember that the name of the script always sys.argv the first parameter of the list. So, here, ' using_sys.py ' is sys.argv[0], ' we ' are sys.argv[1, ' are ' is sys.argv[2 ' and ' arguments ' is sys.argv[3. Note that Python counts starting at 0, not starting at 1.

Sys.argv[] is used to get command-line arguments, Sys.argv[0] represents the file path of the code itself, such as "Python test.py-help" at the cmd command line, and Sys.argv[0] represents "test.py".

Sys.startswith () is used to determine what an object begins with, such as entering ' abc ' in the Python command line. StartsWith (' AB ') returns true

The following examples refer to:

The 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 two characters

if option = = ' Version ':

print ' Version 1.2 '

elif option = = ' help ':

print ' "

This is 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 ""

Else

print ' Unknown option. '

Sys.exit ()

Else

For filename in sys.argv[1:]:

ReadFile (filename)

Execution result: # python test.py--version Help

The 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:] represents from the second argument, from the third character to the end of the intercept, the result of this example 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.