How to save files? i'm so confused

Hi Folks,

I have this python script that I want in include in a snap. Basically, I’m connecting to a outside server (that part works) and I receive a zip file, and I wish to unzip it and save those files across reboots.

This is AWS cert files for an Iot device, so I want these files to be backed up and restored, so SNAP_DATA is what I want to use I think, but have no clue as to go about doing so. Do I refer to SNAP_DATA in the script itself, or just in yaml?

Thanks a million!

Script:

file = open('serial_certs.zip', 'wb+')
while data != bytes(''.encode()):
    file.write(data)
    data = self.sock.recv(1024)

    self.sock.close()

    file.close()

    with zipfile.ZipFile('serial_certs.zip', 'r') as zip_ref:
        zip_ref.extractall(('serial_certs'))

Snapcraft.yaml

apps:
    identity:
       command: python3 $SNAP/tcp_client.py
       daemon: simple
       restart-condition: on-failure
       plugs: [home, network, network-bind]
       environment:
            LC_ALL: C.UTF-8
            LANG: C.UTF-8
parts:
    identity:
       plugin: dump
       source: ./app/identity

SNAP_DATA is available as an environment variable, so in Python you can access it as os.environ['SNAP_DATA']. Alternatively, you could use something like os.path.expandvars('$SNAP_DATA/serial_certs.zip)` to expand the environment variable in a path you provide.