"During handling of the above exception, another exception occurred" - python-3.x

I am reading from a file a list of fractions, and parsing them into a list of tuples. I want an error to occur if the file is empty or if the denominator is less than or equal to 0.
I've tried putting if(EOFERRor), elif(zerodivisionerror), elif(assertionerror), else.. inside the InvalidFile(Exception) class. Before my exception would raise for the end of the file read, so that's why I included that into it specifically.
My guess would be that EOF occurs at the same time as divide by zero, but I separated the list from the file to keep that from happening
class InvalidFile(Exception):
if(EOFError):
pass
else:
print('Invalid file format')
sys.exit(1)
def createFractionList(filePath):
try:
f = open(inFile)
f.close()
except FileNotFoundError:
print('FileNotFoundError')
sys.exit(1)
fractionList = []
for line in open(filePath):
line = line.rstrip()
try:
numerator, denominator = tuple(int(x) for x in line.split())
except:
raise InvalidFile
fractionList.append((numerator, denominator))
for lists in fractionList:
try:
lists[0]/lists[1]
except:
raise InvalidFile
return fractionList
dateList = createFractionList(inFile)
print(dateList)
INPUT:
1 0
3 4
5 6
7 8
9 10
0 8
2 4
9 12
20 24
35 40
54 60
Expected Output:
Invalid file format
Actual Output:
C:\Users\Xavier\PycharmProjects\hw4\venv\Scripts\python.exe C:/Users/Xavier/PycharmProjects/hw4/hw4.py
Traceback (most recent call last):
File "C:/Users/Xavier/PycharmProjects/hw4/hw4.py", line 33, in createFractionList
lists[1]/0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:

The body of your InvalidFile exception does nothing because it's only run at declaration. And in any case, if EOFError is always true, so nothing happens. What you should do is override the __init__ method with a custom text. Personally I don't think you need a custom exception, you can just use in-built ones instead.
Your first error is FileNotFoundError which you'd be better off checking with os.path.exists().
You should use a context manager when you open the file, then you don't need to worry about closing it.
You don't need to strip the line because `int('1 \n') already strips whitespace. But maybe the input file has blank lines, so we could keep this.
You don't need to loop through the file and then the lists, because if at any point, the denominator is less than or equal to 0 the error should be raised. There's no point delaying the error for later. And it seems there is no point doing the division either because you can work out in advance if it's going to fail.
So all in all, you could rewrite the code like this:
import os.path
def create_fraction_list(file_path):
if not os.path.exists(file_path):
raise FileNotFoundError(file_path)
fraction_list = []
with open(file_path) as f:
for line in f:
line = line.strip()
if not line:
# have a blank line
continue
numerator, denominator = (int(x) for x in line.split())
if denominator <= 0:
raise ValueError(f'Denominator less that or equal to 0: {numerator}/{denominator}')
fraction_list.append((numerator, denominator))
if not fraction_list:
# it's empty
raise ValueError(f'Input file has no valid lines: {file_path}')
return fraction_list
if __name__ == '__main__':
try:
fraction_list = create_fraction_list(inFile)
print(fraction_list)
except (FileNotFoundError, ValueError):
print('Invalid file format.')
sys.exit(1)

Related

I/O operation closed on file in python

I have to write a program that prompts the user to enter six test names and their scores and writes them to a text file named tests.txt. You must use a loop. Each input should be written to its own line in the file. The program should generate a confirmation message when done. When I run my program it works but then I get an error at the end saying:
Traceback (most recent call last):
File "C:/Users/brittmoe09/Desktop/program6_1.py", line 34, in <module>
main()
File "C:/Users/brittmoe09/Desktop/program6_1.py", line 18, in main
test_scores.write(name + '\n')
ValueError: I/O operation on closed file.
I am not sure what I am doing wrong, any help would be appreciated.
Here is my code:
def main():
test_scores = open('tests.txt', 'w')
print('Entering six tests and scores')
for count in range(6):
name = input('Enter a test name')
score = int(input('Enter % score on this test'))
while name != '':
test_scores.write(name + '\n')
test_scores.write(str(score) + '\n')
test_scores.close()
print('File was created successfully')
main()
Here's what I did. Get rid of that 2nd while loop, and move the close file out of the for loop because you are closing the file in the loop which is giving you the error: (some of my variable names are different than yours so look out for that)
test_scores = open('tests.txt','w')#open txt file
print('Entering six tests and scores')
for count in range(6):#for loop to ask the user 6 times
name = input('Enter a test name: ')
testscore = int(input('Enter % score on this test: '))
for count2 in range(1):
test_scores.write(str(name) + '\n')
test_scores.write(str(testscore) + '\n')
test_scores.close()#close the txt file
print('File was created successfully!')
the block while:
while name != '':
...
This is an infinity loop if your "name" != '', so in first loop the file closed and second loop you get an error

UnboundLocalError: local variable 'max_results' referenced before assignment

for _ in range(10):
try:
next_results = search_results['search_metadata']['next_results']
except KeyError or e:
break
kwargs = dict([ kv.split('=')
for kv in next_results[1:].split("&") ])
search_results = twitter_api.search.tweets(**kwargs)
statuses += search_results['statuses']
if len(statuses) > max_results:
break
q = "CrossFit"
results = twitter_search(twitter_api, q, max_results=10)
print (json.dumps(statuses[0], indent=1))
The code posted is not what you ran, as search_results is not defined and execution would stop there. My guess, based on what you posted, is that max_results is not defined when len(statuses) > max_results is executed. In your original code, the loop must be within a function definition. The error message can only occur when a name within a function, defined to be local because it is an assignment target, is used before any assignment. For instance:
>>> def f():
if True: return y
else: y = 1
>>> f()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
f()
File "<pyshell#10>", line 2, in f
if True: return y
UnboundLocalError: local variable 'y' referenced before assignment
Please read and act on this SO mcve help page. What you posted is both incomplete and over-complete (everything after the if statement is superfluous).

ZipFile cracker works with a few combinations and crashes when many combinations are used

I've found many examples of zip crackers written in python, but unfortunatelly they were either written in python2 or have the functionality I do not need (i.e. use of dictionaries saved in files). For me was interesting to check how long and how much memory will it take to break, let's say, a password of 5-10 different symbols (a-z, a-zA-Z, a-zA-Z1-10 etc.). Afterwards I can try different libraries and techniques (threads etc.) to improve performance of the code and, hopefully, get better undestaning of python mechanics in the process.
Here is my program. It works well when the program tries 2-position passwords (a-zA-Z) and crashes with longer passwords.
import os
import shutil
import zipfile
from itertools import permutations, combinations_with_replacement
from string import ascii_letters
#passgen() yields passwords to be checked
def passgen(passminlength, passmaxlength,searchdict):
prevpwd = []
for n in range(passminlength,passmaxlength):
for p in combinations_with_replacement(searchdict,n):
for k in permutations(p,n):
pwd_tmp=''.join(k)
if prevpwd != pwd_tmp: #without this check passgen() yields
prevpwd = pwd_tmp #recurring password combinations
yield pwd_tmp #due to the logic behind permutations()
if __name__ == '__main__':
zFile = zipfile.ZipFile("secret.zip", 'r') #encrypted file to crack
pwd = None #password to find
output_directory = os.path.curdir+"/unzip_tmp" #output tmpfolder for extracted files
if not os.path.isdir(output_directory): #if it exists - delete, otherwise - create
os.makedirs('unzip_tmp')
else:
shutil.rmtree(output_directory)
os.makedirs('unzip_tmp')
searchdict = list(ascii_letters) #list with symbols for brute force: a-zA-Z
passminlength = 1
passmaxlength = 3 #code works with passmaxlength=2, doesn't - with passmaxlength=3
pwd_tmp = passgen(passminlength,passmaxlength,searchdict) #pwd_tmp is an iterator
while True:
try:
tmp = next(pwd_tmp)
except StopIteration: #iterator is empty-> quit while-loop
break
print("trying..:%s" % tmp)
zFile.setpassword(bytes(tmp,'ascii'))
try:
zFile.extractall(output_directory)
pwd = tmp
break #password is found->quit while-loop
except RuntimeError: #checked password is wrong ->go again though while loop
print("wrong password:%s" % tmp)
print("password is:%s" % pwd)
The program crashes with maxpasslength=3 on the row with "zFile.extractall(output_directory)".
Error is:
Traceback (most recent call last):
File "C:\Experiments\Eclipse\Workspace\messingaroundpython\zipcracker.py", lin
e 48, in <module>
zFile.extractall(output_directory)
File "C:\Python34\lib\zipfile.py", line 1240, in extractall
self.extract(zipinfo, path, pwd)
File "C:\Python34\lib\zipfile.py", line 1228, in extract
return self._extract_member(member, path, pwd)
File "C:\Python34\lib\zipfile.py", line 1292, in _extract_member
shutil.copyfileobj(source, target)
File "C:\Python34\lib\shutil.py", line 67, in copyfileobj
buf = fsrc.read(length)
File "C:\Python34\lib\zipfile.py", line 763, in read
data = self._read1(n)
File "C:\Python34\lib\zipfile.py", line 839, in _read1
data = self._decompressor.decompress(data, n)
zlib.error: Error -3 while decompressing data: invalid distance too far back
I am stuck. Any idea what I could be missing?
Update: It seems to be a bug from zlib. I added an exeption catcher to ignore bad combinations:
except RuntimeError: #checked password is wrong ->go again though while loop
print("wrong password:%s" % tmp)
except Exception as err:
print("zlib bug, wrong password?:%s" % tmp)
Now the program can process much longer passwords.

Python 3: Checking for the length and data type. & Error Fix

I am having some problems with my project:
With the code I currently have, I am getting an error:
Traceback (most recent call last):
File "\Username\Folder\Folder2\design2.py", line 13, in <module>
elif numberplate3.isaplha():
AttributeError: 'str' object has no attribute 'isaplha'
Here is the code:
while True:
import time
numberplate1 = str(input("Enter in the first 2 letters of the numberplate"))
if numberplate1.isalpha():
print("Verification 1 Success")
numberplate2 = str(input("Enter in the next 2 chars of the numberplate"))
if numberplate2.isdigit():
print("Verification 2 Success")
numberplate3 = str(input("Enter the last 3 digits of the numberplate"))
if numberplate3.isdigit():
print("Verification 3 Fail")
break
elif numberplate3.isaplha():
print("Verification Passed")
start
elif numberplate2.isalpha():
print("Verification 2 Failed")
break
elif numberplate1.isdigit():
print("Return to the start")
break
start = time.time()
inp = input("Please press enter when car has left monitoring area\n>")
end = time.time()
print("Car took {:.2f} seconds ".format(end-start))
print("Car travelled at {:.2f} m/s. ".format(200/(end-start)))
The program will check the format of a numberplate, but I would also like for it to check for the length too. (It checks to see if it has a letter, number etc, but it needs to check for the length on each check)
If possible, a program that checks for the numberplate format would really help.
Checking for LETTER-LETTER-NUMBER-NUMBER LETTER-LETTER-LETTER (AB12 CDE) If not, I am fine with help on my current program
Thanks
Your first problem is just a typo: You misspelled 'isaplha'; it should be 'isalpha'.
For your other question: You can make your program a whole lot simpler by using a regular expression for matching the number plate, like this:
import re
while True:
numberplate = input("Enter the numberplate: ").lower()
if re.match("[a-z]{2}[0-9]{2}[a-z]{3}", numberplate):
print("Verification Success")
break
else:
print("Verification Failed")
Here, the regular expression "[a-z]{2}[0-9]{2}[a-z]{3}" means "two letters, two digits, three letters".

Why won't it write to the file?

So I have the code:
def logdata(x, y):
try:
f = open('multlog.txt', 'a')
f.write("{0:g} * {1:g} = {2:g}\n".format(x,y, (x*y)))
except ValueError:
f.write("Error, you tried to multiply by something that wasn't a number")
raise
finally:
f.close()
print("This is a test program, it logs data in a text file, 'multlog.txt'")
fn = input("Enter the first number you'd like to multiply by: ")
sn = input("Enter the second number you'd like to multiply by: ")
logdata(int(fn), int(sn))
And what I want it to do, is when it reaches a value error, for it to write to the file,"Error, you tried to multiply by something that wasn't a number". But, if the file reaches a value error if the user inputs a letter, say "j",ValueError: invalid literal for int() with base 10: 'j', it doesn't write to the file!
At least two problems:
The file is not open for writing (or appending) in the except block.
As #DSM points out in a comment, the ValueError is being raised when you call int()
I would rewrite to something like the below example.
If you use the with statement then you can do without the finally block.
def logdata(x, y):
with open('multlog.txt', 'a') as f:
try:
x = int(x); y = int(y)
f.write("{0:g} * {1:g} = {2:g}\n".format(x,y, (x*y)))
except ValueError:
f.write("Error")
print("This is a test program, it logs data in a text file, 'multlog.txt'")
fn = input("Enter the first number you'd like to multiply by: ")
sn = input("Enter the second number you'd like to multiply by: ")
logdata(fn, sn)

Resources