Odoo Help


How to open a text file and read it's data in odoo?

Rizan Zaky
on 11/8/15, 11:17 PM 1,728 views

I placed a text file in the same directory as models.py and wrote a file opening and reading code in models.py to open and read the file content. I have set all permissions of the file properly as well. But the file is not read. I'm not sure whether it's a location error. The code works well independently (that means if the python code is run alone, not within odoo). Please help me in this...

Please add your code here so we can see what might be wrong with it.

on 11/9/15, 1:52 AM

I just found the solution for this Yenthe... thanks for the interest :) I'll add it as an answer

Rizan Zaky
on 11/9/15, 1:56 AM

i already given you the test code. You can refer this link :- https://www.odoo.com/forum/help-1/question/load-external-file-script-via-action-button-click-93256

on 11/10/15, 11:47 PM
Rizan Zaky
On 11/9/15, 2:00 AM

I just found the solution, I set the absolute path to where the .txt file is found.

 import os
 path = os.path.expanduser('<absolute path to .txt file>')
 logFile = open(path, 'r')

this worked.

On 11/9/15, 3:47 AM

The solution you found works in your computer, but it'll break if you try to use the module anywhere else. A better solution is to get the path of your module, then append the .txt file name:

def get_txt_path():
directory_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
return os.path.join(directory_path, 'filename.txt')

By the way, unless you have a good reason not to do so, I advise you to use "with open(...) as logFile:" instead of "logFile = open(...)", since it makes sure it'll always close the file, even if there's an exception. Otherwise, you might leak file handles.

I do agree with the statement from app. It is better to get the path of the module and then append, its more dynamic.

on 11/9/15, 5:47 AM

thank you for the information app (Y)... I'll change my code...

Rizan Zaky
on 11/9/15, 6:06 AM

thank you for the information app (Y)... I'll change my code...

Rizan Zaky
on 11/9/15, 6:06 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/8/15, 11:17 PM
Seen: 1728 times
Last updated: 11/10/15, 11:47 PM