IOError: [Errno 13] Permission denied u'data.csv'

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
kage46
Newbie
Posts: 2
Joined: Fri May 03, 2019 5:52 am
Contact:

IOError: [Errno 13] Permission denied u'data.csv'

#1 Post by kage46 » Fri May 03, 2019 6:05 am

Hello Lemma Soft Forums,
I'm currently working on a project and have run into a bit of a problem. The project is used as a means to collect data, which means that it needs to store said data in an external file for me to analyse. I have a script that works perfectly on windows, and does everything I want it to do. The problem arises when I try to make a mac-version of the game, where I get this error: IOError: [Errno 13] Permission denied u'data.csv'
I have tried various attempts at using sudo and chmod but those only give me syntax errors. My script looks as follows:

init python:
try:
prelude = open("data.csv","r")
except IOError:
prelude = open("data.csv","w+")
prelude.write("playerID" + "," + "playerName" + "," + "age" + "," + "gender" + "," +
"opponent" + "," + "choice" + "," + "opChoice" + "," + "condition" + "," + "day" + "," +
"security" + "," + "money" + "," + "food" + "," + "foodThief" + "," + "survivors" + "\n")
prelude.close()

This should create the file if it doesn't already exist. What can I do to possibly make this work on mac?
Thanks in advance,
Tobias

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: IOError: [Errno 13] Permission denied u'data.csv'

#2 Post by rames44 » Fri May 03, 2019 12:07 pm

1. Does the file already exist? If so, what are the file permissions on the ‘data.csv’ file on the Mac? Sounds like the file is either set to be “read only” or else is owned by a user different from the one that Ren’py is running under.
2. If the file doesn’t exist, does the user running the program have write permissions into the directory in which the app is trying to create the file?

kage46
Newbie
Posts: 2
Joined: Fri May 03, 2019 5:52 am
Contact:

Re: IOError: [Errno 13] Permission denied u'data.csv'

#3 Post by kage46 » Sat May 04, 2019 8:35 am

rames44 wrote:
Fri May 03, 2019 12:07 pm
2. If the file doesn’t exist, does the user running the program have write permissions into the directory in which the app is trying to create the file?
The file does not already exist, it's trying to create it, so writing permission is most likely the problem. The question is how to give this permission? I unfortunately do not own a mac myself, so I cant properly troubleshoot. I've tried using sudo and chmod on my end, but those only give me syntax errors when used in renpy.

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: IOError: [Errno 13] Permission denied u'data.csv'

#4 Post by rames44 » Tue May 07, 2019 9:10 pm

So, I downloaded a Ren'py game onto my Mac, extracted it, and looked around. The structure of a Ren'py Mac app is as follows:
The "application" (as is true on all Mac apps) is actually a directory, ending in ".app". The Ren'py files (i.e. .rpa, .rpyc, etc) are located in
theAppName.app/Contents/Resources/autorun/game

All of the directories (the app and its subdirectories) extracted from the ZIP file I downloaded with permissions 755 (i.e. rwxr-xr-x) with my own user ID, which means that, in theory, if the app is running under my user ID, it should be able to write to those directories. That assumes, however, that the "current folder" for a Mac Ren'py app is the "game" folder - if it's not, your application could be trying to write into a place that you can't write without superuser permissions. You could probably figure that out by having some Python code that recorded the current working directory for the Ren'py app and displaying it on the user interface. That would be the first thing I'd experiment with.

In addition, it's possible that you're running afoul of some of the extra protection that recent versions of MacOS have put in place regarding modifications to applications - in the Ren'py app, all the resources are "inside" the application, unlike Windows in which there's an EXE and then external files.

So that's about all I can tell you right now. If I have an opportunity to experiment some more, I will.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: IOError: [Errno 13] Permission denied u'data.csv'

#5 Post by PyTom » Tue May 07, 2019 10:24 pm

Writing files like this isn't supported in Ren'Py - Ren'Py does nothing to enforce the current working directory.

If you want to read the a file, renpy.file is what you want to use to get access it. You could plausibly use Python functions to find a writable location on the user's computer, but that's up to you to understand and implement.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: IOError: [Errno 13] Permission denied u'data.csv'

#6 Post by rames44 » Thu May 09, 2019 6:00 pm

OK, I have an answer - when a Ren'Py game is launched on the Mac (either through Ren'Py itself, or when built as an app and then run), the current working directory is '/' (i.e. the root of the file system)

That directory is owned by "root", not by the user currently running the application, and has permissions "drwxr-xr-x". Thus, non-root users aren't allowed to write into that directory, which completely explains the error that you're getting. (BTW, Linux will almost certainly have the same problem.)

To make this work on the Mac or Linux, you're going to have to use an explicit path to put the file somewhere that's writeable. And, of course, you're going to have to do that differently on each operating system.

Here's a block of code that might help:

Code: Select all

init python:
    import os
    def path_to_save_to():
        if renpy.windows:
            return "./data.csv"
        if renpy.macintosh:
            return os.path.join(os.path.expanduser('~'), "Documents", "data.csv")
        if Ren'Py.linux:
            return os.path.join(os.path.expanduser('~'), "data.csv")
        return None
This returns a string that represents a path to which to save the file:
- On Windows, it saves into the current working directory. I haven't verified where that is, but you presumably already know that
- On the Mac, it saves into the current user's "Documents" folder
- On Linux, it saves into the user's home directory

You can obviously adapt this bit of code to suit your needs.

Hope that helps...

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], span4ev