I’m trying to accomplish a task, but having some difficulty.Can someone set me straight on the following:
#This worked for me
myFormats = {'audio': ('.wav', '.wma', '.mp3'), 'video': ('.mpg', '.mp4', '.mpeg')}
myFile = '5DeadlyVenoms.mp3'
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
extension = f_exten.next()
Using the following resulted in this error:
myFormats = {'audio': {'.wav', '.wma', '.mp3'}, 'video': {'.avi', '.mpg', '.mp4', '.mpeg'}}
Traceback:
Traceback (most recent call last):
File "C:\Users\GVRSQA004\Desktop\udCombo.py", line 65, in fileFormats
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
TypeError: unsupported operand type(s) for +: 'set' and 'set'
Traceback (most recent call last):
File "C:\Users\GVRSQA004\Desktop\udCombo.py", line 65, in fileFormats
f_exten = (x for x in myFormats['audio'] + myFormats['video'] if myFile.endswith(x))
TypeError: unsupported operand type(s) for +: 'set' and 'set'
This is your mistake:
This will always simply return
myFormats['audio'], because this is a logicalor. What you wanted instead was the two tuples appended:An even better solution is using
set‘s and a generator: