I want to check if a text file content has data inside before running some functions. so I use the following Python code
if os.path.exists("userInformation.txt") and os.path.getsize("userInformation.txt") > 0:
with open("userInformation.txt") as info:
contents = info.readlines()
else:
new_file = open("userInformation.txt", "w")
new_file.close()
if file doesn't exist or nothing inside it works fine. However, if a file contains newline, which will be 2 bytes. Then will cause IndexError: list index out of range, after I run some function I know i can use try, except to catch that index error. Is there any other way to it?
Thanks.