Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimetypes.guess_extension : AttributeError: 'NoneType' object has no attribute 'lower' #96264

Closed
HGStyle opened this issue Aug 25, 2022 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@HGStyle
Copy link

HGStyle commented Aug 25, 2022

Hi, when i call the function "guess_extension" from the mimetypes module, i get an error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hg/Bureau/ezConverter/module.py", line 14, in getRealExtension
    currentext = mime.guess_extension(x[0])
  File "/usr/lib/python3.9/mimetypes.py", line 326, in guess_extension
    return _db.guess_extension(type, strict)
  File "/usr/lib/python3.9/mimetypes.py", line 192, in guess_extension
    extensions = self.guess_all_extensions(type, strict)
  File "/usr/lib/python3.9/mimetypes.py", line 171, in guess_all_extensions
    type = type.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

I dont think it's my code. But it is here :

import mimetypes as mime
def getRealExtension(filename):
    x = mime.guess_type(filename)
    currentext = mime.guess_extension(x[0])
# I import it to the shell and call the function.

Thanks in advance !

@HGStyle HGStyle added the type-bug An unexpected behavior, bug, or error label Aug 25, 2022
@ericvsmith
Copy link
Member

Your filename Is probably None. If you can provide a small, complete example that we can run then we can take a look at it, otherwise not much we can do.

@zware zware added the pending The issue will be closed if no feedback is provided label Aug 25, 2022
@zware
Copy link
Member

zware commented Aug 25, 2022

mimetypes.guess_type is documented to return None for the type portion of its return tuple when the type cannot be guessed, and mimetypes.guess_all_extensions (and, thereby, mimetypes.guess_extension) expects a str type argument. There's no bug in the mimetypes module here.

You could work around this by using mimetypes.guess_extension(mimetypes.guess_type(filename)[0] or '').

@zware zware closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2022
@zware zware removed the pending The issue will be closed if no feedback is provided label Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants