データのセキュリティをどの程度確保するかがすべてです。最善の解決策は、暗号化、複数のファイル、またはその両方を使用することです。
ファイル内のデータが正常か安全かを示すためにプログラムで使用できるフラグが必要なだけの場合は、いくつかの方法があります。
- 書き込むたびにヘッダーを追加できます。
- 各行をセキュア レベルを示すフラグで開始し、正しいフラグを持つ行のみを読み取ることができます。
- ファイルの安全な部分とそうでない部分を示すファイル全体のヘッダーを持つことができます。
これは、最初のオプションを使用して実装する方法です。
normal_data = "this is normal data, nothing special"
secure_data = "this is my special secret data!"
def write_to_file(data, secure=False):
    with open("path/to/file", "w") as writer:
        writer.write("[Secure Flag = %s]\n%s\n[Segment Splitter]\n" % (secure, data))
write_to_file(normal_data)
write_to_file(secure_data, True) 
def read_from_file(secure=False):
    results = ""
    with open("path/to/file", "r") as reader:
        segments = reader.read().split("\n[Segment Splitter]\n")
    for segment in segments:
        if "[Secure Flag = %s]" % secure in segment.split("\n", 1)[0]:
            results += segment.split("\n", 1)[0]
    return results
new_normal_data = read_from_file()
new_secure_data = read_from_file(True)
これはうまくいくはずです。しかし、データを保護する最善の方法ではありません。