easy +8 pts

Extract file extensions

Return the extension of a filename, handling edge cases like hidden files and directories.

Write a function `get_extension(filename)` that takes a string `filename` and returns the file extension as a string. The extension is defined as the substring after the LAST dot (`.`) that appears after the last path separator (if any). If there is no dot after the last separator, return an empty string `''`. Detailed rules: - If the filename contains a path (e.g., `'folder/file.txt'`), only consider the part after the last `/`. - If the filename ends with a dot (e.g., `'file.'`), the extension is an empty string `''`. - If the filename starts with a dot (e.g., `'.bashrc'`), the dot is considered part of the name, not an extension separator, so return `''`. - If the filename is exactly `'.'` or `'..'`, return `''`. - If the filename is empty, return `''`. Your function should not rely on any external modules.

Constraints

- 0 <= len(filename) <= 1000 - The string contains only printable ASCII characters (no newline). - Path separators appear as `/` (forward slash).

Example

>>> get_extension('report.pdf')
'pdf'
>>> get_extension('archive.tar.gz')
'gz'
>>> get_extension('folder/file.txt')
'txt'
>>> get_extension('.bashrc')
''
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

First split the filename on `/` and take the last component.
Use `rfind('.')` to find the last dot in that component.
If the dot is at position 0 or absent, return an empty string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.