Odoo Help


How to create a file and save data on it?

Anirudh Lou
on 10/29/14, 5:37 AM 904 views

Hello, Does anyone can help me how to create a file? I have my tree view with data on it. Now i wonder how can i write it on a file so that user can have c copy on it in a .DAT file. Any one knows?

I tried to make a function, but it the web pop an error message:

fo = open(home+"\\persons.DAT","wb")
IOError: [Errno 13] Permission denied: '~user\\persons.DAT'

Here is my method:
    def write_file(self,cr,uid,ids,context=None):
        fo = {}
        home = os.path.expanduser('~user')
        fo = open(home+"\\persons.DAT","wb")
        active_id = self.search(cr,uid,[])
        for list in self.browse(cr,uid,active_id,context=context):
        return fo

Any help is much appreciated.


Ludo - Neobis

--Ludo - Neobis--
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--


Ludo - Neobis
On 10/29/14, 6:47 AM

The user in which OpenERP is running most likely has no access rights to any other directory then the OpenERP directory itself. You will notice for example that files like attachments are usually stored in database OR in the filestore directory under the openerp directory.  Since you are pointing to a home directory, which might be something this user doesn't have. The /tmp directory is also available, but make since its temporary, it is not permanent and therefor not ideal in your situation.

However, there is an alternative to a fysical file. Look up documentation in python on StringIO. This allows you to create a virtual file, and use any normal file operation on it like you normally would. Difference is that this file is not stored on the disk and therefore does not require specific access rights.

Once you have such a file, you store its contents in a binary field in OpenERP and/or as attachment. I think this will work for you.

Thank you sir for your answer, actually i did what you have told me, but i got an error, or perhaps my code is far way from reality. I made a class named 'file.holder' just like this one: class file_holder(osv.osv): _name = 'file.holder' _columns = { 'name' : fields.char('File name'), 'file_data' : fields.binary('File Name',filters='*.DAT',) } def get_file(self, cr, uid, context=None): file_path = addons.get_module_resource('mp_seven', 'files', 'studentlist.DAT') fo = {} fo = open(file_path,"wb") active_id = self.pool.get('student.student').search(cr,uid,[]) for list in self.browse(cr,uid,active_id,context=context): fo.write(list.id+","+list.name) self.create(cr,uid,{ 'name' : '', 'file_data' : file_path }) return fo.read().encode('base64') file_holder() where addons is a folder in openerpserver-7, mp_seven is my project name, files is subfolder in my project and where studentlist.DAT resides. Whenever this method is bieng called in other class the browser/terminal says : file_path = addons.get_module_resource('mp_seven', 'files', 'studentlist.DAT') NameError: global name 'addons' is not defined

Anirudh Lou
on 11/4/14, 3:57 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: 10/29/14, 5:37 AM
Seen: 904 times
Last updated: 1/12/16, 8:41 PM