I want to open a file and check if in that file is an integer and if not write a 0.
I know how to write into a file and read a file, but to check if there is an integer and or there is something else, so I can replace it.
>Solution :
Alternative using .isdigit method of strings. It also tolerates new line \n at the end of the file by stripping it out.
with open("file","r") as fd:
string = fd.read()
if not string.strip().isdigit():
with open("file","w") as fd:
fd.write("0")