python获取启动.exe传递的参数

创建 test.py 文件

import argparse
import time

try:
    parser = argparse.ArgumentParser()
    print(f'parser内容:{parser}')
    parser.add_argument("arg", help="获取参数1")
    parser.add_argument("arg1", help="获取参数2")
    args = parser.parse_args()
    print(f'args内容:{args}')
    print(f'参数1:{args.arg}; 参数2:{args.arg1}')
except BaseException as e:
    print(f'异常错误:{e}')

打包 test.py 

python打包参考:python打包为(.exe)文件_木心操作的博客-CSDN博客

pyinstaller -F test.py

传递参数 

在打包好的 dist 目录执行 cmd 控制面板

在 test.exe 后面加空格传参数【参数之间也要用空格隔开】

注意:接收多少个参数就传多少个参数,否则会报错

 

 python唤醒.exe程序并传递参数

新建一个 test1.py 运行

import subprocess

try:
    # 设置命令行参数
    args = ["argument1", "argument2"]

    # 执行外部程序,并传递参数
    subprocess.run(["F:/test1/dist/test.exe"] + args)
except BaseException as e:
    print(f'{e}')

猜你喜欢

转载自blog.csdn.net/qq_41579327/article/details/131834621