Python Command Line Run File Instance Method

  • 2021-10-15 10:52:23
  • OfStack

1. Description

Write the python file, want to execute it through the command, enter the directory, and enter the python file name in the command execution.

2. Examples

For example, there is a document hello. py on the desk, which is printed with a sentence


print("Hello, Python")

To run it, first enter the Desktop directory, and then type pythonhello. py on the command line to run:


Solo-mac:Desktop solo$ python hello.py
Hello, Python

Extension of knowledge points:

It is common to run python code on the command line, so here's how to define the parameters that follow the command.

Conventional usage


import argparse 
 
parser = argparse.ArgumentParser("Description.") #  --- 1 --- 
parser.add_argument('--test1', type=str, default="1", help="Test1 help.") #  --- 2 --- 
parser.add_argument('--test2', type=float, default=2.1, help="Test2 help.") #  --- 2 --- 
args = parser.parse_args() #  --- 3 --- 

print(args)
test1, test2 = args.test1, args.test2 #  --- 4 ---  
print(type(test1), test1, test1 + " add test") 
print(type(test2), test2, test2 + 100)

Note:

1. Get an instance of an external parameter object. You can pass in a string to describe the external parameters of the population.

2. Define external parameters, which can be called many times to define multiple external parameters. The first parameter of the function is the name of the external parameter. It is recommended to start with two minus signs, and do not use symbols other than numeric alphanumeric underscores in the middle, otherwise it is easy to make mistakes. type is the type you want to convert when an external parameter is passed in. For example, test1 is defined as str, and even if the command line parameter is passed without quotation marks, the function will convert it to a string (of course, you can't use spaces when passing a string without quotation marks). And--test2 is float, and functions can be converted to float as long as they can be converted to numbers, even if the command-line arguments are quoted. default is the default value for external parameters. If the command line does not assign values to external parameters and uses default values, the function will not convert the default values you define on type, so it is better to define default directly as the type specified by type (for example, default of test1 is defined as "1" instead of 1). help passes in a description of the current external parameter, which is what the command line uses--help outputs.

3. Analyze the acquired external parameters. Outgoing is a namespace, similar to a dictionary, but using attributes instead of key indexes to get the external parameter values corresponding to the names.

4. Get external parameters by getting attributes.

It should be noted that the definition of external parameters must be written before obtaining external parameters. That is, parser.add_argument () should be written before parser.parse_args (), otherwise external parameters cannot be captured by the command line. In addition, the command line cannot pass parameters without parser.parse_args () in the code.


Related articles: