How to read $SNAP_DATA from python script?

Hi all!

I am quite new to Snapcraft and I’m working on a simple python project. In my project I have a configure hook that creates a file in the $SNAP_DATA location. How would I read this created file from within my python script?

I’ve tried the following without success sadly:

snap_data = os.environ['SNAP_DATA']
config_file = open(str(snap_data) + "/config.txt", "r")

Thank you in advance :slight_smile:

That code looks okay, what is the exact error you get ? And are you sure the file has actually been created.

1 Like

Yes the file does exist. Could it have something to do with my snap being a daemon? I get: FileNotFoundError: [Errno 2] No such file or directory: ‘/var/snap/hello/171/config.txt’

Well, the error indicates it does not exist… $SNAP_DATA points to the correct dir (/var/snap/<snapname>/<snaprevision>)…

Is your configure hook using $SNAP_DATA as well to write the file initially?

How can one determine the location and structure of $SNAP_DATA from a Python script to effectively read and manipulate data?

$SNAP_DATA is an environment variable which tells you the location where snap has write access, it’s a simple directory. The contents and structure of what exists at that location is up to your application.