Python ARVG usage

Source: Internet
Author: User
Tags readfile

Transferred from: http://blog.csdn.net/vivilorne/article/details/3863545

In the process of learning Python, I never understood the meaning of sys.argv[], though I knew it was a command line argument, but it was a bit of a blur.

Today, a good study of a, finally is Taichetaiwu.

Sys.argv[] is used to get command line arguments, Sys.argv[0] represents the code itself file path, so the parameters starting from 1, the following two examples illustrate:

1, using a simple example of sys.argv[],

Import Sys,os

    1. Os.system (Sys.argv[1])

This example Os.system receive command line arguments, run parameter directives, save as sample1.py, command line with parameters run sample1.py Notepad, will open the Notepad program.

2, this example is a concise Python tutorial, understand it after you understand sys.argv[].

[python] view plain copy 
  1. Import Sys
  2. def readfile (filename): #从文件中读出文件内容
  3. ' Print a file to the standard output. '
  4. f = file (filename)
  5. While True:
  6. line = F.readline ()
  7. If Len (line) = = 0:
  8. Break
  9. Print line, # Notice comma output each row of content separately
  10. F.close ()
  11. # Script starts from here
  12. If Len (SYS.ARGV) < 2:
  13. print ' No action specified. '
  14. Sys.exit ()
  15. If Sys.argv[1].startswith ('--'):
  16. option = sys.argv[1][2:]
  17. # fetch SYS.ARGV[1] But without the first and the characters
  18. if option = = ' Version ': #当命令行参数为--version, display the revision number
  19. print ' Version 1.2 '
  20. elif option = = ' help ': When #当命令行参数为--help, the relevant helper content is displayed
  21. Print ""/
  22. Prints files to the standard output.
  23. Any number of the files can be specified.
  24. Options include:
  25. --version:prints the version number
  26. --help:display this ""
  27. Else
  28. print ' Unknown option. '
  29. Sys.exit ()
  30. Else
  31. For filename in sys.argv[1:]: #当参数为文件名时, incoming ReadFile, read out its contents
  32. ReadFile (filename)

Save the program as sample.py. Let's verify that:

1) command line with parameters run: Sample.py–version output is: Version 1.2

2) command line with parameters run: Sample.py–help output is: This program prints files ...

3) in the same directory as sample.py, create a new a.txt Notepad file with the content: Test argv; command line with parameters run: sample.py a.txt, the output is a.txt file content: Test argv, here can also take a few more parameters, The program will output the contents of the parameter file successively.

~~~~~~~~~~~~believe yourself, nothing was impossible, write in 02.05.2009 by vivilorne~~~~~~~~~~~~~

Python ARVG usage

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.