I wrote some code for a python file scanner, but the only problem is my custom error codes won't run! I have to use 1 and 0 for the true and false because python3 doesn't accept true and false! Here's the code.
#File Scanner 1.2
scanTXTlog = input("Please type what you are looking for")
file = input("Please enter the .txt name")
#Gives new error messages
if UnicodeDecodeError == 1:
print("Unicode error.....error resolved")
if FileNotFoundError == 1:
print(file, 'could not be found. Please make sure you spelled it correctly and please do not add the .txt extension.')
#Begins searching
txtFile = open(file + ".txt", "r")
lineList = []
i = 0
for line in txtFile:
lineList.append(line.rstrip("\n"))
if scanTXTlog in lineList[i]:
print(lineList[i -2 ])
i += 1
****UPDATE BECAUSE I'M NEW TO STACK AND HAVE NO IDEA HOW TO REPLY****
I tried some new stuff and it works...but only on certain files. It can scan lots of them, but sometimes it just hits one and gives the IO error after the program is done. Here's the new code
def fileRead(encoding= 'utf-8'):
scanTXTlog = input("Please type what you are looking for")
file = input("Please enter the file name. Capitalization does not matter. Please add extension such as .py or.txt etc, etc")
#Begins searching
txtFile = open(file, "r", encoding = 'utf-8')
lineList = []
i = 0
for line in txtFile:
lineList.append(line.rstrip("\n"))
if scanTXTlog in lineList[i]:
print(lineList[i])
i += 1
class UnicodeDecodeErrorHandler(encoding = 'utf-8'):
def UnicodeDecodeErrorDefault(issubclass):
print('Done')