#!/usr/bin/env python3 # ファイルに値を保存するカウンタ(Python) # ~yas/syspro/file/file-counter-python.rb import sys DATA_FILENAME = "file-counter-value.data" comname = None def usage(): print(f"Usage$ {comname} {{init,get,up,set newvalue}}") print(f"Data file: {DATA_FILENAME}"); exit(1) def main(argv): global comname comname = argv[0] if len(argv) <= 1: usage() cmd = argv[1] if cmd == "init": counter = 0 elif cmd == "get": counter = counter_load() elif cmd == "up": counter = counter_load() counter += 1 elif cmd == "set": if len(argv) != 3: usage() newvalue = int(argv[2]) counter = newvalue else: usage() print(counter) if cmd != "get": counter_save(counter) return def counter_load(): with open(DATA_FILENAME, "rb") as f: counter_bytes = f.read(4) counter = int.from_bytes(counter_bytes, byteorder=sys.byteorder) return counter def counter_save(counter): counter_bytes = counter.to_bytes(4, byteorder=sys.byteorder) with open(DATA_FILENAME, "wb") as f: f.write(counter_bytes) return main(sys.argv)