I have a class where all of our data is in, here I open a file by:
carIn= open("dataIn.txt","w") carOut= open("dataUit.txt","w") In another class I have a loop for the main program. I closed my file in the loop but it won't open again. If I close it outside the loop, the entire program crashes. This is my code:
while startScreen.getstopt() == False: startScreen.start() print("screen start") screen = Screen(wereld.getIntersection()) startSimulatiion() print("Simulatiion start:") for x in Files.listIn: Files.carIn.write(str(x) + "\n") for x in Files.listOut: Files.carOut.write(str(x) +"\n") result= Resultaten() Files.calculatedRatio= result.calculateRatio() print(Files.calculatedRatio) if screen.startScreen == True: Files.carIn.write("\n") Files.carIn.write("\n") Files.carIn.write("\n") Files.carOut.write("\n") Files.carOut.write("\n") Files.carOut.write("\n") Files.carIn.close() Files.carOut.close()
with openso you don't need to worry about close the file. You can read more about it here. Also, you can create a class to abstract it, for example, you call thewritemethod, the class will open the file, write and close the file.