Python small white (no programming foundation, no computer basis) development of the road auxiliary knowledge 5 SYS.ARGV

Source: Internet
Author: User

A concise explanation of the use of sys.argv[] in Python

Because it is reading self-taught python, began soon after the introduction of the module function, and has been on the idle edit after the run, trying to find its use from the results, but the results have been no results, but also on the internet a lot, but found that the problem of a more detailed explanation of only one version, Most of them are transferred and copied. Give is a concise Python tutorial on that long string of code examples, said to understand, but I see the dizzy or not really understand, only know "sys.argv[0" means the code itself file path "This point, in fact, it is unclear meaning." Later, after a lot of efforts, the multi-advice is really enlightened, I would like to record and share, hoping to from another point of view to the same students in the process of seeking a little inspiration.

Sys.argv[] Plainly is a bridge from the outside of the program to get parameters, the "Outside" is critical, so those who try to explain its role from the code has not been understood. Because the arguments we get from the outside can be multiple, we get a list, which means that sys.argv can actually be seen as a list, so we can use [] to extract the elements. The first element is the program itself, followed by the externally given parameters.

Below we illustrate its usage with a very simple test.py program running results.

Import Sys

A=SYS.ARGV[0]

Print (a)

Save the test.py in the root directory of the C drive.

In the program, locate the ' run ', click-to-enter "cmd" and return Enter the console command window (e.g.), first enter the CD c \ (The function is to change the command path to the C packing directory) and then enter test.py to run the program we just wrote:

The result is C:\test.py, which is the meaning of the 0-finger code (the. PY program) itself.

Then we change the code from 0 to 1:

A=SYS.ARGV[1]

After saving, and then running from the console window, this time we add a parameter, enter: test.py what

The result is the parameter we entered. What, see here you're starting to understand.

Then let's change the code a bit:

A=sys.argv[2:]

After saving, then run the program from the console sill, this time add several parameters, separated by a space:

test.py a b c d E F

The results are [' B ', ' C ', ' d ', ' e ', ' F ']

It should be taichetaiwu. SYS.ARGV is actually a tuple, inside the item for the user input parameters, the key is to understand that this parameter is input from the outside of the program, and not the code itself where, to see its effect should be saved, from the outside to run the program and give parameters.

Python small white (no programming foundation, no computer basis) development of the road auxiliary knowledge 5 SYS.ARGV

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.