How to write a python file with command-line arguments

Source: Internet
Author: User
Tags python script

When someone else executes a python file that supports command-line arguments, and instantly feels tall and up, how do you write a Python script with command-line arguments? Without straining, here's a quick and easy way to learn how to get your own Python script to support command-line arguments.

First you need to know some of the functions of the SYS module in python:

ImportSYSPrint "The number of the Python program ' s argument:", Len (SYS.ARGV)Print "The value of every argument is", str (SYS.ARGV)#the file name of the above program sysargv.pypython sysargv.py argv1 argv2 argv3 argv4the number of Python program's Argument:5The value of every argument is['sysargv.py','argv1','Argv2','Argv3','argv4']

Second, the Python program uses command-line arguments, the necessary module, is the Getopt module, first look at a piece of code

getopt. getopt ( args, options[, long_options])
ImportGetoptargs='-a-b-cfoo-d bar A1 A2'. Split () args[' -A','- b','-cfoo','- D','Bar','A1','A2']optlist, args= Getopt.getopt (args,'abc:d:') optlist[(' -A',"'), ('- b',"'), ('- C','Foo'), ('- D','Bar')]args['A1','A2']

Using long_options

s ='--condition=foo--testing--output-file abc.def-x A1 A2'args=s.split () args['--condition=foo','--testing','--output-file','Abc.def','- x','A1','A2']optlist, args= Getopt.getopt (args,'x', ['condition=','output-file=','Testing']) optlist[('--condition','Foo'), ('--testing',"'), ('--output-file','Abc.def'), ('- x',"')]args['A1','A2']

Finally, an example of the actual combat!

ImportGetopt,sysdefMain ():Try: Opts,args=getopt.getopt (sys.argv[1:],"hi:o:v",[" Help","infile=","outfile="])    exceptgetopt. Getopterror as Error:PrintStr (Error) Usage () Sys.exit (2) infile=None Output=None verbose=False forKey,valueinchopts:ifkey=="- v": Verbose=TrueelifKeyinch("- H","--help"):            Print "sysargv.py-i <inputfile> o <outputfile>"            Print "or sysargv.py--infile <inputfile>--outfile <outputfile>"        elifKeyinch("- I.","--infile"): infile=valueelifKeyinch("- o","--outfile"): Output=valuePrint "Inputfile:", infilePrint  "outputfile:", OutputPrintverboseif __name__=="__main__": Main ()

Test results:

C:\python27>python sysargv.py--helpsysargv.py-I <inputfile>-o <outputfile>orsysargv.py--infile <inputfile>--outfile <outputfile>inputfile:noneoutputfile:nonefalsec:\python27>python sysargv.py-hsysargv.py-I <inputfile>-o <outputfile>orsysargv.py--infile <inputfile>--outfile <outputfile>inputfile:noneoutputfile:nonefalsec:\python27>python Sysargv.py-i"Inputfile1"-O"Ouputfile2"inputfile:inputfile1outputfile:ouputfile2falsec:\python27>python Sysargv.py-i"Inputfile1"inputfile:inputfile1outputfile:nonefalsec:\python27>python Sysargv.py-o"Outputfile1"inputfile:noneoutputfile:outputfile1falsec:\python27>python Sysargv.py-o"Outputfile1"-vinputfile:noneoutputfile:outputfile1truec:\python27>python sysargv.py--infile"Inputfile"--outfile"Outputfile1"-vinputfile:inputfileoutputfile:outputfile1True

How to write a python file with command-line arguments

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.