Skip to content Skip to sidebar Skip to footer

Files In Python Are Empty For No Reason

I have tried to split up my world generation from my actual game, since I usually fail with it. But for some reason it keeps insisting the file is empty/ the variable gained from i

Solution 1:

You probably have empty lines in your input file; you would want to skip these.

You can also simplify your tile reading code:

withopen("C:\Users\Ben\Documents\Python Files\PlatformerGame Files\World.txt", "r") as world_file:
    WORLD = [Tile(*line.strip().split(":", 1)) for line in world_file if':'in line]

This only processes lines if there is a : character in them, splits only once, and creates the WORLD list in one loop.

As for using os.startfile(): you are starting the other script in the background. That script then opens the file to write to and explictly empties the file, before it generates new data. At the same time you are trying to read from that file. Chances are you end up reading an empty file at that time, as the other process hasn't yet finished generating and writing the data, and as file writes are buffered you won't see all of the data until the other process closes the file and exits.

Don't use os.startfile()at all here. Import the other file instead; then the code will be executed during import and the file is guaranteed to be closed.

Post a Comment for "Files In Python Are Empty For No Reason"