opening text files

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
User avatar
usul
Veteran
Posts: 415
Joined: Mon Oct 29, 2007 12:35 pm
Projects: Teachings of the Buddha, System-Addict, Generation XxX
Location: Quebec
Contact:

opening text files

#1 Post by usul »

I'm trying to write to a text file, but for some reason it creates this text file in my home folder instead of creating it in my game folder. Is there a way to tell it to write to the game folder in a way that will work regardless of the OS of the end user after the game is published?

Code: Select all

python:
        f = open('texts/somefile.txt', 'w')
        f.write('Hello, ')
        f.write('World!')
        f.close()
"The universe is non-simultaneously apprehended"
— Buckminster Fuller

User avatar
PyTom
Ren'Py Creator
Posts: 16097
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: opening text files

#2 Post by PyTom »

Fundamentally, no. You can look at config.gamedir, but that directory may not be writable.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
usul
Veteran
Posts: 415
Joined: Mon Oct 29, 2007 12:35 pm
Projects: Teachings of the Buddha, System-Addict, Generation XxX
Location: Quebec
Contact:

Re: opening text files

#3 Post by usul »

What is config.gamedir? Is there another way to handle data (written to disk) with Ren'Py?
"The universe is non-simultaneously apprehended"
— Buckminster Fuller

User avatar
PyTom
Ren'Py Creator
Posts: 16097
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: opening text files

#4 Post by PyTom »

config.gamedir is the path to the game/ directory. Apart from the standard python ways, there isn't a way to write data to disk.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
usul
Veteran
Posts: 415
Joined: Mon Oct 29, 2007 12:35 pm
Projects: Teachings of the Buddha, System-Addict, Generation XxX
Location: Quebec
Contact:

Re: opening text files

#5 Post by usul »

I don't know where to look for config.gamedir? I don't see it in the options file.
"The universe is non-simultaneously apprehended"
— Buckminster Fuller

User avatar
PyTom
Ren'Py Creator
Posts: 16097
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: opening text files

#6 Post by PyTom »

Ren'Py sets it automatically - you can just use it.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
usul
Veteran
Posts: 415
Joined: Mon Oct 29, 2007 12:35 pm
Projects: Teachings of the Buddha, System-Addict, Generation XxX
Location: Quebec
Contact:

Re: opening text files

#7 Post by usul »

can you give an example? I'm clueless as to where to put it and how it works.
"The universe is non-simultaneously apprehended"
— Buckminster Fuller

User avatar
PyTom
Ren'Py Creator
Posts: 16097
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: opening text files

#8 Post by PyTom »

Code: Select all

python hide:
    import os
    fn = os.path.join(config.gamedir, 'myfile.txt')
    f = file(fn, "w")
    f.write('Hello, World.\n")
    f.close()
[code]
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]