问题
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