2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

python3で抽選ツールを作りました。作ったツールをタスクスケジューラーで決まった時間に自動で動かすため、コマンドプロンプト化しました。

1.コマンドプロンプトのソースコード

実装したコマンドプロンプトのソースコードは以下の通りです。

@echo off

:: ============================================================
:: selection_tool1.py 起動用バッチ
:: Create Date : 2026/07/31
:: Thing   : selection_tool_new2.py を実行する
:: ============================================================


:: バッチファイルの配置フォルダへ移動
cd /d "%~dp0"

:: Pythonスクリプトの存在確認
if not exist selection_tool_new2.py (
    echo Not exist selection_tool_new2.py
    exit /b 1
)

::Pythonスクリプトを実行
python selection_tool_new2.py

:: 実行結果確認用
notepad result.txt

2.pythonのソースコード

実装したpythonのソースコードは以下の通りです

import random
import os


# 結果出力ファイル名
result_file = "result.txt"

# 担当者格納用変数
monday_in_charge = ""
tuesday_in_charge = ""


def main():
    # 既存の結果ファイルがある場合は削除
    if os.path.exists(result_file):
        os.remove(result_file)

    # 朝会ファシリテーター候補者一覧
    members = [
        "kaho",
        "ayaka",
        "moe",
        "satsuki"
    ]

    monday_in_charge = random.choice(members)
    tuesday_in_charge = random.choice(
        [member for member in members if member != monday_in_charge]
    )

    
    # 結果をテキストファイルへ出力
    with open(result_file, 'a') as fs:

        # 月・水・金担当者
        print(f"月、水、金の朝会のファシリ担当は{monday_in_charge}さんです",file=fs)

        # 火・木担当者
        print(f"火、木の朝会のファシリ担当は{tuesday_in_charge}さんです",file=fs)


if __name__ == "__main__":
    main()        

3.実行結果

実行結果は以下の画像です

result1.jpg

最後に

Windows11のタスクスケジューラーで決まった時間に自動実行したかったので、作ってみました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?