list - Files in python are empty for no reason -
i have tried split world generation actual game, since fail it. reason keeps insisting file empty/ variable gained empty, , sometimes, when afterwards, actual program file has emptied text file info, not. here code:
here's small extract of file handling in main code:
world_file = open("c:\users\ben\documents\python files\platformergame files\world.txt", "r") world_file_contents = world_file.read() world_file.close() world_file_contents = world_file_contents.split("\n") world = [] data in world_file_contents: usable_data = data.split(":") world.append(tile(usable_data[0],usable_data[1]))
and tile class:
class tile(): def __init__(self,location,surface): self.location = location self.surface = surface
and error:
traceback (most recent call last): file "c:\users\ben\documents\python files\platformergame", line 89, in <module> game.__main__() file "c:\users\ben\documents\python files\platformergame", line 42, in __main__ world.append(tile(usable_data[0],usable_data[1])) indexerror: list index out of range
sorry if it's obvious. i'm using pygame.
you have empty lines in input file; want skip these.
you can simplify tile reading code:
with open("c:\users\ben\documents\python files\platformergame files\world.txt", "r") world_file: world = [tile(*line.strip().split(":", 1)) line in world_file if ':' in line]
this processes lines if there :
character in them, splits once, , creates world
list in 1 loop.
as using os.startfile()
: starting other script in background. script opens file write , explictly empties file, before generates new data. at same time trying read file. chances end reading empty file @ time, other process hasn't yet finished generating , writing data, , file writes buffered won't see all of data until other process closes file , exits.
don't use os.startfile()
at all here. import other file instead; code executed during import , file guaranteed closed.
Comments
Post a Comment