#!/usr/bin/env python3 from sys import argv rows = {} with open("students.textdb", "rt") as file: for line in file.readlines(): idx, lab, proj, exam = line.strip().split(" ") rows[int(idx)] = (idx, lab, proj, exam) # argv is like # ["set_student_1.py", "512345", "5.0", "3.0", "3.0"] rows[int(argv[1])] = tuple(argv[1:5]) with open("students.textdb", "wt") as file: for _, (idx, lab, proj, exam) in sorted(dict.items(rows)): # simulate slow disk writes from time import sleep sleep(0.15) print(idx, lab, proj, exam, file=file)