Chaquopy: error while reading a text file from python code “No such file or directory”

只愿长相守 提交于 2021-01-28 03:30:24

问题


I am using python as a backend code for some scientific programs in the android app. For that, I am using the Chaquopy plugin. I found an error "No such file or directory" when accessing the text file which is stored at "app/src/main/python/". I think this is a path issue.


回答1:


In Chaquopy 6.3.0 and later, you can access data files using a path relative to __file__. For example, if the data file is in the same directory as the Python file:

from os.path import dirname, join
filename = join(dirname(__file__), "filename.txt")

If the data file and the Python files are in different directories, then change the relative path accordingly. For example, if the Python file is src/main/python/alpha/hello.py, and the data file is src/main/python/bravo/filename.txt, then replace filename.txt with ../bravo/filename.txt.

Source: https://github.com/chaquo/chaquopy/issues/144



来源:https://stackoverflow.com/questions/58832006/chaquopy-error-while-reading-a-text-file-from-python-code-no-such-file-or-dire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!