Python argv usage and 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:
#-*- coding:utf- -*-from sys import argvscript,first = argvprint "the script is called:", scriptprint "the first variable is:", first
The result is as follows:
Here argv receives a list variable.
#-*- coding:utf- -*-from sys import argvf = open(argv[], 'r')print f.read()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 ].
Next we will introduce the usage of sys. argv [] in detail.
Sys. argv [] is used to obtain the command line parameters. sys. argv [0] indicates the file path of the Code. Therefore, the parameter starts from 1. The following two examples illustrate:
1. Use a simple instance of sys. argv,
import sys,os os.system(sys.argv[1])
In this example, OS. system receives command line parameters, runs parameter commands, and saves them as sample1.py. The command line runs sample1.py notepad with parameters, and opens the notepad program.
2. This example is a concise python tutorial. After you understand it, you will understand sys. argv.
Import sys def readfile (filename): # Read the file content from the file ''' Print a file to the standard output. '''f = file (filename) while True: line = f. readline () if len (line) = 0: break print line, # notice comma outputs each line of content f. close () # 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 first two characters if option = 'version': # When the command line parameter is -- version, print 'version 1.2 'elif option = 'help': # When the command line parameter is -- help, print '''/This program prints files to the standard output. any number of 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:]: # When the parameter is a file name, input readfile and read its content readfile (filename)
Save the program as sample. py. Let's verify it:
1) run the command line with parameters: sample. py-version. The output result is: version 1.2
2) run the command line with parameters: sample. py-help. The output result is: This program prints files ......
3) in the same directory as sample.py, the.txt notepad file is created with the following content: test argv. Run the command line with parameters: sample. py a.txt, the output result is a.txt file content: test argv. Here you can also include several more parameters. The program will output the parameter file content successively.
Articles you may be interested in:
- Python command line parameter sys. argv
- Example of using the python command line parameter sys. argv
- Example Analysis of sys. argv parameter usage in python
- Python sys. argv usage example