@@ -140,11 +140,27 @@ def get_file_name(file_path: StrPath, include_extension: bool = True) -> str:
140140
141141 E.g. /tmp/foo.bar -> foo
142142
143- URLs use the last non-empty segment of the path component, ignoring any query string or
144- fragment, so https://example.com/videos/clip.mp4?token=1.2 -> clip. If the URL has no path
145- (e.g. rtsp://192.168.0.1:554), the host is used instead, with the port separator replaced
146- so the result is a valid file name on all platforms. Returns an empty string only if no
147- usable name can be derived."""
143+ URL and URI-like inputs:
144+ Inputs containing "://" are treated as URLs (see :func:`is_url`) and handled by their
145+ URL components rather than as paths, since a path separator inside a query string or
146+ fragment is not part of the name. The name is the last non-empty segment of the path
147+ component, percent-decoded, e.g. https://example.com/videos/clip.mp4?token=1.2 -> clip.
148+
149+ When the path component is empty there is no segment to use, so the host becomes the
150+ name, e.g. rtsp://192.168.0.1:554 -> 192.168.0.1_554. Userinfo (credentials) is never
151+ part of the result, and the port is joined with "_" because ":" is not valid in a file
152+ name on Windows. Hosts are returned in full even when `include_extension` is False, as
153+ the last "." in a host is a domain separator and not an extension.
154+
155+ Arguments:
156+ file_path: Path, or URL, to get the name of.
157+ include_extension: If False, remove the extension (the last "." and everything after
158+ it) from the name. Has no effect on names derived from a URL host.
159+
160+ Returns:
161+ The name, or an empty string if none can be derived (e.g. a path that is only
162+ separators, or a URL with neither a path nor a host).
163+ """
148164 file_path = os .fspath (file_path )
149165 if is_url (file_path ):
150166 parsed = urllib .parse .urlsplit (file_path )
@@ -154,7 +170,7 @@ def get_file_name(file_path: StrPath, include_extension: bool = True) -> str:
154170 # host has no extension to remove: stripping after the last "." would turn
155171 # example.com into example.
156172 host = parsed .hostname or ""
157- return f"{ host } _{ parsed .port } " if parsed .port else host
173+ return f"{ host } _{ parsed .port } " if host and parsed .port else host
158174 file_name = segments [- 1 ]
159175 else :
160176 file_name = os .path .basename (file_path )
0 commit comments