🙆‍♀️

【Python】引数を指定して PythonでBat or Shell を実行!

に公開

概要

BatまたはShellに引数をつけて実行するPythonコード
提携業務向け・・・

サンプルコード

import subprocess

def execute_script(script_path, *args):
    """
    バッチファイルまたはシェルスクリプトを引数付きで実行する。
    :param script_path: 実行するスクリプトのパス
    :param args: 最大5個の引数を指定可能
    """
    if len(args) > 5:
        raise ValueError("引数は最大5個まで指定可能です。")
    
    command = [script_path] + list(args)
    result = subprocess.run(command, capture_output=True, text=True)
    
    print(f"コマンド: {' '.join(command)}")
    print(f"出力: {result.stdout}")
    print(f"エラー: {result.stderr}")

# 使用例
# ここに実行したいパスを入力
A = "/home/xxxx/script.bat(sh)"
# ここで引数を指定(最大5個まで)
execute_script(A, "arg1", "arg2", "arg3", "arg4", "arg5")

補足

・事前にコードを編集してパスと引数を登録しておく必要がある。

Discussion