we dont know how your data look like:

But you can use re.sub:

import re
your_string = re.sub(r'[\\/*?:"<>|]',"","your_string")
Answer from Hackaholic on Stack Overflow
🌐
Medium
medium.com › @ryan_forrester_ › remove-special-characters-from-strings-in-python-complete-guide-53651c8163d9
Remove Special Characters from Strings in Python: Complete Guide | by ryan | Medium
January 7, 2025 - def clean_filename(filename): # Remove characters that are invalid in file names invalid_chars = '<>:"/\\|?*' for char in invalid_chars: filename = filename.replace(char, '') return filename.strip() # Example: Cleaning user-submitted file names dirty_filename = "My:Cool*File.txt" clean_name = clean_filename(dirty_filename) print(clean_name) # Output: "MyCoolFile.txt" def create_url_slug(text): # Convert to lowercase and replace spaces with hyphens slug = text.lower().strip() # Remove special characters slug = re.sub(r'[^a-z0-9\s-]', '', slug) # Replace spaces with hyphens slug = re.sub(r'\s+', '-', slug) # Remove multiple hyphens slug = re.sub(r'-+', '-', slug) return slug # Example: Creating a URL-friendly slug article_title = "10 Tips & Tricks for Python Programming!" url_slug = create_url_slug(article_title) print(url_slug) # Output: "10-tips-tricks-for-python-programming"
Discussions

Py - Clean illegal character in filename
Hi, I’m doing a production tool which copy files between folders in our pipeline, on Windows 10. My problem is that some files have been renamed with copy/paste by some people, and it seems to add illegal characters in the file names. Those characters are invisible in explorer.exe: The illegals ... More on tech-artists.org
🌐 tech-artists.org
0
0
August 29, 2019
python - Removing characters from filename in batch - Stack Overflow
I have 3 main folder in Windows explorer that contain files with naming like this ALB_01_00000_intsect_d.kml or Baxters_Creek_AL_intsect_d.kml. Even though the first name changes the consistent th... More on stackoverflow.com
🌐 stackoverflow.com
batch rename - Removing special characters from filenames in multiple subdirectories in Python - Stack Overflow
I have a root directory called rootDir, and under that multiple sub-directories called subDir1, subdir2, etc. All sub-directories contain hundreds of files. I would like to remove some special (b... More on stackoverflow.com
🌐 stackoverflow.com
October 23, 2015
Best Way to Remove Special Characters from Filenames?
Wow, that script is filthy. You should start by getting a list of all the files and saving it to a variable instead of immediately piping into the ether. Then, you can use that variable to filter and only run the rename on files you actually want to rename. Look into foreach and where-object to get the filter really cranking. Once you nail the filter, then you start worrying about renaming. You are moving too many steps ahead without getting the fundamentals down. Stop and think first. More on reddit.com
🌐 r/PowerShell
35
5
December 22, 2023
Top answer
1 of 16
307

You can look at the Django framework (but take their licence into account!) for how they create a "slug" from arbitrary text. A slug is URL- and filename- friendly.

The Django text utils define a function, slugify(), that's probably the gold standard for this kind of thing. Essentially, their code is the following.

import unicodedata
import re

def slugify(value, allow_unicode=False):
    """
    Taken from https://github.com/django/django/blob/master/django/utils/text.py
    Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
    dashes to single dashes. Remove characters that aren't alphanumerics,
    underscores, or hyphens. Convert to lowercase. Also strip leading and
    trailing whitespace, dashes, and underscores.
    """
    value = str(value)
    if allow_unicode:
        value = unicodedata.normalize('NFKC', value)
    else:
        value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub(r'[^\w\s-]', '', value.lower())
    return re.sub(r'[-\s]+', '-', value).strip('-_')

And the older version:

def slugify(value):
    """
    Normalizes string, converts to lowercase, removes non-alpha characters,
    and converts spaces to hyphens.
    """
    import unicodedata
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
    value = unicode(re.sub('[-\s]+', '-', value))
    # ...
    return value

There's more, but I left it out, since it doesn't address slugification, but escaping.

2 of 16
198

You can use list comprehension together with the string methods.

>>> s
'foo-bar#baz?qux@127/\\9]'
>>> "".join(x for x in s if x.isalnum())
'foobarbazqux1279'
🌐
CodeVsColor
codevscolor.com › python program to remove special characters from all files in a folder - codevscolor
Python program to remove special characters from all files in a folder - CodeVsColor
May 4, 2019 - #1 import os from os import listdir ... you can see that the file names are changed in the folder. All special characters, numbers and spaces are removed from the files....
🌐
Medium
medium.com › @blueberry92450 › three-ways-to-remove-special-characters-from-string-in-python-da1035cc93b8
Three ways to Remove Special Characters from String in Python Including Time Comparison | Medium
August 8, 2022 - Using replace built in method in python requires a customized function. def replace_symbol(filename): for symbol in ['*', '?', '%', '&', '$', '(', ')', '#', '^', '@', '!', '~', '-', '+', '=', " ", ",", "'", '"',"/", "."]: if symbol in filename: filename = filename.replace(symbol, '') return filename
🌐
Tech-Artists.Org
tech-artists.org › coding
Py - Clean illegal character in filename - Coding - Tech-Artists.Org
August 29, 2019 - Hi, I’m doing a production tool which copy files between folders in our pipeline, on Windows 10. My problem is that some files have been renamed with copy/paste by some people, and it seems to add illegal characters in the file names. Those characters are invisible in explorer.exe: The illegals characters are between ‘0’ and ‘3’ (you should be able to cc them): pixPath = u"E:/col_udim_test.100​​​​​3.jpg" os.rename and shutil.copy give errors when trying to handle the files: os.rename(path ...
🌐
Python Forum
python-forum.io › thread-32534.html
Rename Multiple files in directory to remove special characters
The purpose of the script below is to rename multiple folders in a directory. File is named 2324[folder 1], 3242343[Folder 2), 4343[folder 3]. The purpose of script is to rename files to keep only folder 1, folder 2, and folder 3 name. I currently ha...
Find elsewhere
🌐
Reddit
reddit.com › r/powershell › best way to remove special characters from filenames?
r/PowerShell on Reddit: Best Way to Remove Special Characters from Filenames?
December 22, 2023 -

I've got a bunch of files (mostly screenshots from Premiere Pro and MPC-HC but some other things too) that I need to use with other programs, but they don't like the special characters those programs write into their filenames, especially brackets.

Basically, I want to batch rid filenames of anything that's not A-Z, 0-9, ",", "-" or "_". Ideally I'd like to replace brackets and parentheses with "_" while I'm at it.

The script that I have now basically gets me there, but it's cumbersome when I'm working with large file groups:

Get-ChildItem -LiteralPath $sourceDir -File -Recurse | Rename-Item -NewName { $_.Name -replace "[^\d+a-z+\s+&',.!-_()]"} -Verbose
Get-ChildItem -Literalpath $sourceDir -File -Recurse | Rename-Item -NewName { $_.Name -replace "[\[\]]" } -Verbose
Get-ChildItem -Literalpath $sourceDir -File -Recurse | Rename-Item -NewName { $_.BaseName.replace("."," ") + $_.Extension } -Verbose

So like I said...this works, but it requires running the GCI call multiple times, and it renames EVERY file, not just ones that fit the criteria (which isn't what I want).

So I'm asking for suggestions on better ways to go about this. I really want to tell GCI to identify any and all files with anything other than letters and numbers and dashes and commas, but I don't know how to do that.

Any suggestions?

🌐
TradingCode
tradingcode.net › python › sanitise-clean-filename
How to fix (sanitise) invalid filenames with Python?
Python automatically sanitises (cleans) filenames with the sanitize_filename() function. This function from the pathvalidate package makes a filename valid by, among other things, removing invalid characters and reserved words from the filename.
🌐
Make Community
community.make.com › questions
Remove special characters from file name - Questions - Make Community
November 22, 2022 - Hi all. I am looking for a way to remove special characters from a text string. I would like to use a file name input and then remove ~ " # % & * : ? / \ { | }. if they exist in the file name and return the text without those characters. I’m trying to use the Text Parser - Replace but can’t ...
🌐
Stack Overflow
stackoverflow.com › questions › 71149107 › remove-special-character-from-multiple-file-names-in-folder-in-python
Remove special character from multiple file names in folder in python - Stack Overflow
February 16, 2022 - import os # I named my folder containing .XML files "Test" Folder = 'Test/' for filename in os.listdir(Folder): os.rename(Folder + filename, Folder + filename.replace('-',''))
🌐
Ask Ubuntu
askubuntu.com › questions › 1178988 › how-do-i-remove-special-characters-from-file-name-with-a-command
scripts - How do I remove special characters from file name with a command? - Ask Ubuntu
You can use rename rename -n -E 's/(¿|¡)//' *.mp4. -n just prints the result without actually do anything (remove it when You are ready to rename files). Just put more characters to remove inside the brackets () and join them with pipe |. ...
🌐
Reddit
reddit.com › r/learnpython › how to delete special characters in the title of a file?
r/learnpython on Reddit: How to delete special characters in the title of a file?
June 19, 2022 -

Hi, basically in my python script i'm using to learn I'm trying to select the title of a web page and then save a txt file with the same name of the title of the webpage. The only problem is, if the title of the web page contains special characters such as : then the name of the txt is truncated just before this special character. How do I avoid this problem? I'm fine with completely deleting the special character from the name of the txt file.

I have tried this, during the selection

p.title = title_page['title'] + contents.replace(':','')

and also the same at the end, during the save of the file, but it doesn't work, it tells me contents is not defined in both situations. The problem is I cannot define it since the special character may be there but it may also NOT be there. How do I fix this?

Thanks

🌐
Ask Ubuntu
askubuntu.com › questions › 1334884 › remove-certain-special-characters-from-file-names
rename - remove certain special characters from file names - Ask Ubuntu
April 29, 2021 - Note that . is not a special character in the context of filenames so I'd suggest not actually replacing that. ... thank you. I found that there's a program in ubuntu software called bulk rename which worked to remove the character, but I used perl rename as well.
🌐
Quora
quora.com › How-do-I-remove-special-characters-from-multiple-filenames-quickly
How to remove special characters from multiple filenames quickly - Quora
Answer (1 of 3): hard to answer generally. I’d probably go a like 1. open a cmd and navigate to the wanted directory 2. generate a file with just the filenames one per line ( dir/b /a-d > filerename.bat [/b only filenames /a-d no directories)) 3. open this file in a good texteditor (for example ...
Top answer
1 of 5
49

I think the safest approach here is to just replace any suspicious characters. So, I think you can just replace (or get rid of) anything that isn't alphanumeric, -, _, a space, or a period. And here's how you do that:

import re
re.sub(r'[^\w_. -]', '_', filename)

The above escapes every character that's not a letter, '_', '-', '.' or space with an '_'. So, if you're looking at an entire path, you'll want to throw os.sep in the list of approved characters as well.

Here's some sample output:

In [27]: re.sub(r'[^\w\-_\. ]', '_', r'some\*-file._n\\ame')
Out[27]: 'some__-file._n__ame'
2 of 5
29

Unfortunately, the set of acceptable characters varies by OS and by filesystem.

  • Windows:

    • Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
      • The following reserved characters are not allowed:
        < > : " / \ | ? *
      • Characters whose integer representations are in the range from zero through 31 are not allowed.
      • Any other character that the target file system does not allow.

    The list of accepted characters can vary depending on the OS and locale of the machine that first formatted the filesystem.

    .NET has GetInvalidFileNameChars and GetInvalidPathChars, but I don't know how to call those from Python.

  • Mac OS: NUL is always excluded, "/" is excluded from POSIX layer, ":" excluded from Apple APIs
    • HFS+: any sequence of non-excluded characters that is representable by UTF-16 in the Unicode 2.0 spec
    • HFS: any sequence of non-excluded characters representable in MacRoman (default) or other encodings, depending on the machine that created the filesystem
    • UFS: same as HFS+
  • Linux:
    • native (UNIX-like) filesystems: any byte sequence excluding NUL and "/"
    • FAT, NTFS, other non-native filesystems: varies

Your best bet is probably to either be overly-conservative on all platforms, or to just try creating the file name and handle errors.