Search code examples
djangofileioerror

Django production: [Errno 13] Permission denied:


In my Django app I have this kind of error: "IOError: [Errno 13] Permission denied: 'file_name'"

This is my code:

def record_export():
    for file_name, tab_name in tab:
        if len(globals()[tab_name].objects.all()) <> 0:
            f = open(file_name, 'wb')
            writer = csv.writer(f, delimiter='|')
            for record in globals()[tab_name].objects.values_list():
                writer.writerow([unicode(s).encode("utf-8") for s in record]) 
            f.close()

In development enviroment all it's ok. I think that I have the permission. In production I have: "IOError: [Errno 13] Permission denied: 'file_name'" Do you know why? Thanks for your help


Solution

  • Djangos's runserver usually runs as root, this is probably your problem.

    Your webserver needs rights to read/write the file. You can use ls -l /your/path/to/file to check permissions for a given directory. To change rights and owner, use chmod and chown.

    If you are running a apache2 webserver your user and group is in most cases www-data.