Skip to content

Commit

Permalink
added util to remove special chars in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed Jun 4, 2024
1 parent f0953b0 commit 9e6facb
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,18 @@ def url_is_valid(url: str) -> bool:
r'(?:/?|[/?]\S+)$' # /, /path, or /path?query=string
, re.IGNORECASE)

return True if url_regex.match(url) else False
return True if url_regex.match(url) else False

def remove_special_chars(filename, replacement=""):
"""Removes special characters from a filename and replaces them with a chosen character.
Args:
filename: The filename to be sanitized.
replacement: The character to replace special characters with (default: "_").
Returns:
The sanitized filename.
"""
# Regular expression to match special characters (excluding alphanumeric, underscore, and dot)
pattern = r"[^\w\._]+"
return re.sub(pattern, replacement, filename)

0 comments on commit 9e6facb

Please sign in to comment.