How to use os.system in a strict python snap

Hi, i’m trying to do a strict python snap that run on a Raspberry Pi 4 with Ubuntu Core 20.04 . I made my python snap in confinement devmode to test it, and it works. Now i want to convert it in a strict snap but when i run the snap i see the following error:

ls: cannot open directory '.': Permission denied
cp: cannot create regular file './Config_HRM_WB-RD_V3_4-64_AGC.json': Permission denied

The reference python code is the following (in the code i want to copy a json file from a folder in the current folder):

#!/usr/bin/env python3

import sys
import os

os.system('ls -l')
os.system('cp $SNAP/lib/python3.8/site-packages/iot-band/Config_HRM_WB-RD_V3_4-64_AGC.json .')


This is my snapcraft file (the daemon line is commented out because after solving this problem I want my snap to be a daemon):

name: iot-band
base: core20
version: '1.0'
summary: Iot band app
description: |
  Iot band app

grade: devel
confinement: strict

      PYTHONPATH: $SNAP/lib/python3.8/site-packages/iot-band
    command: bin/
    # daemon: simple
      - bluez
      - network
      - home

    # See 'snapcraft plugins'
    source: .
    plugin: python
      - .
    plugin: nil
    stage-snaps: [bluez/edge]
    plugin: nil
    stage-snaps: [pi-bluetooth/edge]
      - lib64/*

I think it is an interface problem but i don’t know how to fix it. How can i realize correctly the snap to resolve this problem?

cp $SNAP/lib/python3.8/site-packages/iot-band/Config_HRM_WB-RD_V3_4-64_AGC.json . copies that file to current directory, but your snap does not necessarily have write (or even read) access to that location. You need to account for this in your code.

Also, the home interface is not auto connected, so unless you have a snap declaration from the store to allow this, you’d have to manually run snap connect iot-band:home. (You can still check for this interface being connected and print out a message to the user).

See for the locations where your snap can always write data.