勤怠記録アプリ 1日目のゴール
1日目のテーマは 「出勤時刻・退勤時刻を記録する最小の勤怠アプリを作りながら、変数・条件分岐・繰り返し・関数・ファイル操作・datetime の基礎を体験すること」 です。
勤怠記録は初心者にとって非常に学びやすい題材です。 理由はシンプルで、
- 時刻という“扱いやすいデータ”を使う
- datetime の学習に最適
- 記録 → 保存 → 表示 の流れが明確
- 後日「勤務時間計算」「月次集計」「遅刻判定」などに発展できる
というメリットがあるからです。
1日目で作るアプリのイメージ
- 出勤時刻を記録
- 退勤時刻を記録
- 記録をファイルに保存
- 記録一覧を表示
この流れを作ることで、 変数・条件分岐・繰り返し・関数・ファイル操作・datetime をすべて体験できます。
datetime とは何か(初心者向けにかみ砕いて)
datetime は 「日付や時刻を扱うための標準ライブラリ」 です。
勤怠記録では、次のような情報を扱います。
- 今日の日付
- 出勤時刻
- 退勤時刻
- 勤務時間(後日扱う)
datetime の基本例
from datetime import datetime
now = datetime.now()
print(now)
Python深掘りポイント
datetime.now()→ 現在時刻を取得- 時刻は「文字列」ではなく「時刻オブジェクト」として扱う
- 後日、計算(勤務時間など)が簡単になる
勤怠記録を保存するファイル形式
今回は CSV風のテキスト形式 を使います。
例:
2026-08-10,09:00:12,18:05:33
- 日付
- 出勤時刻
- 退勤時刻
をカンマ区切りで保存します。
出勤時刻を記録する関数
from datetime import datetime
def record_start():
"""
出勤時刻を記録する
"""
now = datetime.now()
date_str = now.strftime("%Y-%m-%d")
time_str = now.strftime("%H:%M:%S")
with open("attendance.txt", "a", encoding="utf-8") as f:
f.write(f"{date_str},{time_str},\n")
print(f"出勤時刻を記録しました: {date_str} {time_str}")
Python深掘りポイント
strftime()→ datetime を文字列に変換- 出勤時刻だけ先に記録し、退勤時刻は後で追記する
- ファイル操作
"a"→ 追記モード
退勤時刻を記録する関数
退勤時刻は「最後の行の末尾に追記」します。
def record_end():
"""
退勤時刻を記録する
"""
now = datetime.now()
time_str = now.strftime("%H:%M:%S")
# ファイルを読み込み
with open("attendance.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
if not lines:
print("出勤記録がありません。先に出勤を記録してください。")
return
# 最後の行を編集
last = lines[-1].strip()
if last.endswith(","):
# 出勤のみ記録されている場合
new_last = last + time_str + "\n"
lines[-1] = new_last
else:
print("すでに退勤時刻が記録されています。")
return
# 上書き保存
with open("attendance.txt", "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"退勤時刻を記録しました: {time_str}")
Python深掘りポイント
- 出勤 → 退勤 の順番を守る
- 最後の行を編集するために「読み込み → 書き込み」
endswith(",")で「退勤未記録」を判定
勤怠記録一覧を表示する関数
def show_records():
"""
勤怠記録一覧を表示する
"""
try:
with open("attendance.txt", "r", encoding="utf-8") as f:
print("=== 勤怠記録一覧 ===")
for line in f:
date, start, end = line.strip().split(",")
print(f"日付: {date} / 出勤: {start} / 退勤: {end}")
print("====================")
except FileNotFoundError:
print("まだ勤怠記録がありません。")
Python深掘りポイント
- ファイルがない場合は例外処理
split(",")でデータを分解- 繰り返し処理で一覧表示
メニューを表示する関数
def show_menu():
print("------------------------------")
print("勤怠記録アプリ 1日目 メニュー")
print("1: 出勤時刻を記録する")
print("2: 退勤時刻を記録する")
print("3: 勤怠記録一覧を表示する")
print("4: 終了する")
print("------------------------------")
Pythonmain(アプリ全体の流れ)
def main():
print("Python 勤怠記録アプリ 1日目")
print("出勤・退勤を記録できる最小の勤怠アプリを作ります。")
while True:
show_menu()
choice = input("番号を入力してください(1〜4): ")
if choice == "1":
record_start()
elif choice == "2":
record_end()
elif choice == "3":
show_records()
elif choice == "4":
print("アプリを終了します。")
break
else:
print("1〜4 の番号を入力してください。")
Python1日目の完成コード(まとめ)
from datetime import datetime
def record_start():
now = datetime.now()
date_str = now.strftime("%Y-%m-%d")
time_str = now.strftime("%H:%M:%S")
with open("attendance.txt", "a", encoding="utf-8") as f:
f.write(f"{date_str},{time_str},\n")
print(f"出勤時刻を記録しました: {date_str} {time_str}")
def record_end():
now = datetime.now()
time_str = now.strftime("%H:%M:%S")
with open("attendance.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
if not lines:
print("出勤記録がありません。")
return
last = lines[-1].strip()
if last.endswith(","):
lines[-1] = last + time_str + "\n"
else:
print("すでに退勤時刻が記録されています。")
return
with open("attendance.txt", "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"退勤時刻を記録しました: {time_str}")
def show_records():
try:
with open("attendance.txt", "r", encoding="utf-8") as f:
print("=== 勤怠記録一覧 ===")
for line in f:
date, start, end = line.strip().split(",")
print(f"日付: {date} / 出勤: {start} / 退勤: {end}")
print("====================")
except FileNotFoundError:
print("まだ勤怠記録がありません。")
def show_menu():
print("------------------------------")
print("勤怠記録アプリ 1日目 メニュー")
print("1: 出勤時刻を記録する")
print("2: 退勤時刻を記録する")
print("3: 勤怠記録一覧を表示する")
print("4: 終了する")
print("------------------------------")
def main():
print("Python 勤怠記録アプリ 1日目")
print("出勤・退勤を記録できる最小の勤怠アプリを作ります。")
while True:
show_menu()
choice = input("番号を入力してください(1〜4): ")
if choice == "1":
record_start()
elif choice == "2":
record_end()
elif choice == "3":
show_records()
elif choice == "4":
print("アプリを終了します。")
break
else:
print("1〜4 の番号を入力してください。")
if __name__ == "__main__":
main()
Python1日目で本当に掴んでほしいこと
● datetime は「時刻を扱うための標準ライブラリ」
勤怠記録のような“時刻を扱うアプリ”では必須。
● ファイル操作は「アプリに記憶を持たせる技術」
勤怠記録が残ることで、アプリが“道具”として使えるようになります。
● 関数を分けることでアプリが整理される
出勤・退勤・一覧・メニュー・main が役割ごとに分かれ、理解しやすくなります。
次のステップ(2日目の予告)
2日目では、勤怠記録アプリをさらに育てて、
- 勤務時間の計算
- 遅刻判定
- 早退判定
- 日付指定で検索
という「勤怠管理の基本形」を完成させていきます。

