使用1和0因为python3不接受true和false?

时间:2014-10-05 16:49:39

标签: python

我为python文件扫描程序编写了一些代码,但唯一的问题是我的自定义错误代码无法运行!我必须使用1和0表示true和false,因为python3不接受true和false!这是代码。

#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

****更新,因为我是新的堆叠,没有想法如何回复****

我尝试了一些新东西并且它有效......但仅限于某些文件。它可以扫描很多,但有时它只会命中一个并在程序完成后给出IO错误。这是新代码

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')

2 个答案:

答案 0 :(得分:3)

..... What?

try:
  PossiblyRaiseAnException()
except SomeException:
  HandleException()
except SomeOtherException as e:
  HandleOtherException(e)

答案 1 :(得分:3)

首先,Python 3 支持支持TrueFalse

要处理Errors,您必须构建可能将错误引发到try-except的所有内容 - 条款:

try:
    txtFile = open(file + ".txt", "r")
except OSError:
    print(file, 'could not be found. Please make sure you spelled it correctly and please do not add the .txt extension.')

访问docs,了解有关错误和例外的详情。