Kivy python - getting "FileNotFoundError: [Errno 2] No such file or directory: '/home/me/BUILDOZER_TEST/countries.txt'

Viewed 30

I am using Buildozer to package a python script into an APK. When I install the APK on an Android emulator, and run it, I am getting "FileNotFoundError". I simply want to read in a file containing a list of countries (countries.txt) and then list these in a DropDown menu.

Based on advice I saw online about adding a "datas" line in the "Analysis" section, I have edited buildozer.spec, adding the following block:

a = Analysis(['main.py'],
         pathex=[''], (not clear what to put here??)
         hiddenimports=[],
         hookspath=None,
         datas=[('/home/me/BUILDOZER_TEST/countries.txt')], (this is the file to read)
         runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      name='Main',
      debug=False,
      strip=None,
      upx=True,
      console=False , icon='mainIcon.ico')

In main.py I have tried giving the open() command with the full path. I have also tried os.chdir("/the/path/where/the/file/is") as seen below:

def __init__(self, **kwargs):
    super().__init__(**kwargs)

    self.cols = 2  # used for our grid

    cwd = os.getcwd()  # Get the current working directory (cwd)
    files = os.listdir(cwd)  # Get all the files in that directory
    print("Files in %r: %s" % (cwd, files))

    os.chdir(r'/home/me/BUILDOZER_TEST/')
    with open('./countries.txt') as f:
        content = f.readlines()
        li = [x.strip() for x in content]
        print(li)

When I use os.chdir(), it says file or directory does not exist. Is my buildozer.spec file not correct? Not sure what to try next... Any and all advice appreciated.

0 Answers
Related