Page 1 of 1

Problem with file("whatever.txt").read()

Posted: Tue Mar 20, 2007 6:21 pm
by Ivlivs
I tried to include these lines of code into my work:

Code: Select all

$ name = file("Your name.txt").read()
$ s = Character('Someone', color="#c8ffc8")
$ r = Character(name, color="#c8ffc8")
Then I get a traceback error message stating that it cannot find the file "Your name.txt", even though the thing is in the directory with the rest of the script.

How do I fix this issue?

Posted: Tue Mar 20, 2007 9:27 pm
by Ivlivs
I figured out the problem: you have to put the text file in the main Ren'Py directory (where the launcher is) rather than inside the game directory itself.

I have no idea how to make it read from the game directory.

Secondly, when I try to place into the variable a text file containing a single name in Japanese and nothing else, then call the variable using '%(variable)s', I get an error indicating that something is out of range.

The error looks something like this:

Code: Select all

I'm sorry, but an exception occured while executing your Ren'Py
script.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

While executing game script on line 20 of F:\VN\renpy-6.1.0\The Sandbox/game/script.rpy.

-- Full Traceback ------------------------------------------------------------

  File "renpy-6.1.0\renpy\bootstrap.py", line 166, in bootstrap
  File "renpy-6.1.0\renpy\main.py", line 275, in main
  File "renpy-6.1.0\renpy\main.py", line 92, in run
  File "renpy-6.1.0\renpy\execution.py", line 97, in run
  File "renpy-6.1.0\renpy\ast.py", line 250, in execute
  File "renpy-6.1.0\renpy\exports.py", line 461, in say
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

Posted: Tue Mar 20, 2007 9:39 pm
by PyTom
Try

$name = file(config.gamedir + "/Your Name.txt").read()

You need to tell it explicitly to look in the gamedir. To fix the encoding bug, you need to explicitly specify the encoding. The only one we really support is utf-8, so you want to save the text file as uf-8, and then write:

$ file(config.gamedir + "/Your Name.txt").read().decode("utf-8")