Search code examples
androidandroid-studiochaquopy

How do I use a pathname in chaquopy python?


So, from the docs I figured out how to "hard code" files into android studio (In the androidstudioprojects directory basically) and can successfully access them from my python script. (using

join(dirname(test.__file__), filename)

) etc etc

But, I need to be able to have users upload something to the app, the app creates its own directory in the phone's external storage, places the files they uploaded there and I want to be able to pass in one of those file's paths into my python script and it can successfully retrieve them. I want to pass in something like:

/data/user/0/com.myapp.blah/mycustomdir/myfilename.xlsx

And my python script would be able to pull that file and work on it. How do I go about doing this?

Edit: It looks like I have it, but I need to make sure I did it right. I will leave this up because from searching the chaquopy tag I could not find anyone else asking this type of question so maybe it will help others. For me it seems to be as simple as taking that above filepath and inserting it in like:

"/data/user/0/com.myapp.blah/mycustomdir/" + filename

I guess you could pass the entire filepath into your script but for me I thought to pass just the filename in so I did that and it seems to have worked, without the need for the join or dirname functions.


Solution

  • If you already know the path in the Java part of your app, then you can just pass it to Python as a string parameter: nothing special is needed.

    Or if you want to construct one of these paths in the Python part of your app, then the best way is to do something like this:

    from com.chaquo.python import Python
    context = Python.getPlatform().getApplication()
    

    You can then call any of the Context methods like getFilesDir, getExternalFilesDir, etc.