Python's simplest use of argv, pythonargv

Source: Internet
Author: User

Python's simplest use of argv, pythonargv

If you want to use python to process files and find argv usage, you can find it.

If you want to pass parameters in python, you need to support the command line parameters. This saves you every time you make changes.

Usage: python xx. py xxx

Example:

1 #-*-coding: UTF-8-*-2 3 from sys import argv4 5 script, first = argv6 print "the script is called:", script7 print "the first variable is: ", first

The result is as follows:

 

Here argv receives a list variable.

1 #-*-coding: UTF-8-*-2 3 from sys import argv4 5 f = open (argv [1], 'R') 6 print f. read () 7 f. close ()

For example, if I read the file name and start to write it as open (argv, 'R'), a type error will be prompted, just change it to argv [1 ].

 

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.