Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading/Loading a file #144

Closed
kongkip opened this issue Sep 6, 2019 · 7 comments
Closed

Reading/Loading a file #144

kongkip opened this issue Sep 6, 2019 · 7 comments

Comments

@kongkip
Copy link

kongkip commented Sep 6, 2019

How can i read a file in the project directory with python?
i.e.

with open('chatbot.txt','r', encoding='utf8', errors ='ignore') as fin:
    raw = fin.read().lower()

or where should put it for easier loading

@mhsmith
Copy link
Member

mhsmith commented Sep 6, 2019

In Chaquopy 6.3.0 and later, you can access data files using a path relative to __file__.

[Example moved to the documentation]

@mhsmith mhsmith closed this as completed Sep 6, 2019
@kongkip
Copy link
Author

kongkip commented Sep 6, 2019

Thanks for that, it worked with version 6.3.0.

@Leandropslv
Copy link

Leandropslv commented Nov 1, 2019

I want to understand why it's not working here, I have a .txt file inside the same folder as the .py file. I use this code:

from os.path import dirname, join
with open(join(dirname(file), 'chatbot.txt'), ...

And this error is returned:
com.chaquo.python.PyException: FileNotFoundError: [Errno 2] No such file or directory: '/android_asset/chaquopy/app.zip/csa.txt'

@mhsmith
Copy link
Member

mhsmith commented Nov 2, 2019

As mentioned above, you must be using Chaquopy 6.3.0 or later for this technique to work. Check the version number of com.chaquo.python:gradle in your project's top-level build.gradle file.

@Leandropslv
Copy link

Leandropslv commented Nov 4, 2019

[Moved to #160]

@chaquo chaquo deleted a comment from Leandropslv Nov 4, 2019
@chaquo chaquo deleted a comment from Leandropslv Nov 4, 2019
@mhsmith mhsmith mentioned this issue Mar 8, 2020
@mhsmith mhsmith mentioned this issue Mar 17, 2020
This was referenced May 26, 2020
@AbsolutelySaurabh
Copy link

AbsolutelySaurabh commented Jul 26, 2020

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__), "chatbot.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 chatbot.txt with ../bravo/chatbot.txt.

@mhsmith you rocked man, this worked after sucking up for days.

@sarthaksharma058439
Copy link

Finally I got a solution after a long time, this guy explains in simple way how to open and read file data using chaquopy in android studio Check this link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants