一、参数的二种传入方式
1.终端传入
示例:以位置参数为例
比如下面这段代码
import argparse
# 创建 ArgumentParser 对象
parser = argparse.ArgumentParser()
# 添加位置参数
parser.add_argument("source", help="path to source file")
parser.add_argument("destination",type=str,help="path to destination file")
# 解析命令行参数
args = parser.parse_args()
# 使用位置参数的值
print("Source:", args.source)
print("Destination:", args.destination)
在终端输入:
1.首先找到终端
2.在终端中切换到自己所在的位置
3.输入自己的脚本和参数,用空格隔开。
输出结果:
Source: mul
Destination: m
2.找到IDE自带的输入参数位置
用pycharm举例:
1.首先找到Edit Configuretions
2.可以在Parameters中输入参数
比如我这里输入mul m
输出结果:
Source: mul
Destination: m