I have a simple txt-file which consists of a number of rows. Something like below
Name1
0
0
0
0
Name2
0
0
0
0
Name3
...
...
...
(By ... I mean that the file continues in the same way until infinity hypothetically)
Now my question is, how do I create some kind of a "file control"? I want the file to be used only IF the file outline is of the form presented above.
My current piece of code consists of a Try-Except part as below:
def foo
try:
content = open("filename.txt")
except:
file1 = [name1,0,0,0,0,name2,0,0,0,0....]
save(file1)
content = open("filename.txt")
lscontent = content.read().splitlines()
file.close()
What the function save() does is pretty obvious. It only creates and saves the list to a file in the form that I want it to be. Something like initial values. My trouble is that if the user changes the file format or if the file becomes corrupt, then the program will most probably crash.
Any simple ideas which will do this control before launching the program itself? If file is corrupt, I want to replace the file with the initial values, which means I want the file control to be in the try part of the function foo. Ofcourse I want the correct file to have that structure as above.