python load problem...

i have a text file:


-f
1024 768
1

and i wand to load every single line as string… and this is not working:


for x in range(len(windowed)):
	loadWindowed = loadFile.readline()
	windowed = (loadWindowed[0:-1])
settings["windowed"] = windowed

for x in range(len(resolution)):
	loadResolution = loadFile.readline()
	resolution = (loadResolution[0:-1])
settings["resolution"] = resolution

please help

Try this:

# Open file with universal newline support
my_file = open("file_location", "rU")

# Read all lines of file
file_data = my_file.readlines()

# Load the data in the correct order, removing trailing whitespace
settings['windowed'] = file_data[0].rstrip()
settings['resolution'] = file_data[1].rstrip()

my_file.close()

Loading the file in mode “rU” enables universal newline support, so your game will be more cross-platform. The rstrip() method removes trailing whitespace, including newline characters. If you only want to remove newline characters, but leave trailing spaces and tabs, you can use rstrip("
")

hey many thanks to you! it’s working now!

This module might also interest you:
http://docs.python.org/library/configparser.html

Oh, now that is cool. And it only requires re, so for Windows Blender 2.49b games, you could literally just drop it into the Python2.6.zip package in your game distribution folder and be good to go. Wow. I cannot adequately express how excited I am about this.

I used the configparser module for a save/load system I was working on, so much easier being able to call specific variables instead of having to read through every line (especially when lots of data is saved). You can do it with less code as well.

it’s cool but i dont store many data in this file only resolution fullscreen/windowed, framerate show/hide and sound on/off… so i dont want to modify the script if it is working :smiley: and for save load game i’m using Monsters save/load script with my modifications…