So, I am trying to use a try and except statement to see if both functions are able to return one of the preset extensions in the list.
Output if I input "file.zip"
Application/zip
image() missing 1 required positional argument: 's'
Same error, if I was to do a say like "file.jpg"
What I am wanting to achieve by this is for if it is not able to detect one of the extensions in the list after looping over to throw the error ‘application/octet-stream’
I was wanting to attempt to do this outside of each function, instead of making one large function as practice, not sure if that is valid practice or not. Also would like to do it without importing any type of libraries, this isn’t meant to have files actually imported into the program just simple practice with looping over a list and detecting if the specific extension is located in the list.
def main():
file = input("File name:")
image(file)
application(file)
try:
if image() == False:
raise Exception('application/octet-stream') # Error is here.
if application() == False:
raise Exception('application/octet-stream')
except Exception as E:
print(E)
def image(s):
split = (s.split("."))
join_s = (''.join(split[1]))
image_e = ['jpg', 'gif', 'jpeg', 'png']
for i in image_e:
if join_s in image_e:
print("Image/"+join_s)
return True
def application(s):
split = (s.split("."))
join_s = (''.join(split[1]))
app_e = ['pdf', 'txt', 'zip']
for i in app_e:
if join_s in app_e:
print("Application/"+join_s)
return True
main()
I also tested it by having the file variable, as an argument in the try statement, and it doesn’t throw any sort of error if I give it an extension not located in the list.
>Solution :
The reason you are getting an error is that inside your try block you are calling image (and application) with no parameters, when they expect one. You should get rid of the calls to image and application above the try and call them in there instead.
Other issues:
- if the file is not an image, an exception is issued before testing whether it is an application
- your functions don’t have a return value when they don’t match, which means they return
None, notFalse. - the data passed to
raiseis inE.args, notE - it’s generally bad practice to name variables after functions (
split)
You could resolve these issues with this code:
def main():
file = input("File name:")
try:
if not image(file) and not application(file):
raise Exception('application/octet-stream') # Error is here.
except Exception as E:
print(E.args[0])
Additionally your functions could be greatly simplified e.g.
def image(s):
image_e = ['jpg', 'gif', 'jpeg', 'png']
ext = s.split(".")[1]
if ext in image_e:
print("Image/"+ext)
return True
return False
Finally I would say that although this is an interesting exercise, it would be better to use a single function for all files, replacing the image_e etc. variables with a dictionary e.g.
{
'Image' : ['jpg', 'gif', 'jpeg', 'png'],
'Application' : ['pdf', 'txt', 'zip']
}
and then looking up the extension in the values and using the key as the MIME type.