To secure uploaded image names, I'd like to strip out image's filenames from anything but string.ascii_letters , string.digits, dot and (one) whitespace. 
So I'm wondering what is the best method to check a text against other characters?
To secure uploaded image names, I'd like to strip out image's filenames from anything but string.ascii_letters , string.digits, dot and (one) whitespace. 
So I'm wondering what is the best method to check a text against other characters?
 
    
    import re
import os
s = 'asodgnasAIDID12313%*(@&(!$ 1231'
result = re.sub('[^a-zA-Z\d\. ]|( ){2,}','',s )
if result =='' or os.path.splitext(result)[0].isspace():
    print "not a valid name"
else:
    print "valid name"
EDIT:
changed it so it will also whitelist only one whitespace + added import re
 
    
    Not sure if it's what you need but give it a try:
import sys, os
fileName, fileExtension = os.path.splitext('image  11%%22.jpg')
fileExtension = fileExtension.encode('ascii', 'ignore')
fileName = fileName.encode('ascii', 'ignore')
if fileExtension[1:] in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'tga']:
    fileName = ''.join(e for e in fileName if e.isalnum())
    print fileName+fileExtension
    #image1122.jpg
else:
    print "Extension not supported"
isalnum()
 
    
    I wouldn't use regex for this. The only tricky requirement is the single space, but that can be done, too.
import string
whitelist = set(string.ascii_letters + string.digits)
good_filename = "herearesomelettersand123numbers andonespace"
bad_filename = "symbols&#! and more than one space"
def strip_filename(fname, whitelist):
    """Strips a filename
    Removes any character from string `fname` and removes all but one
    whitespace.
    """
    whitelist.add(" ")
    stripped = ''.join([ch for ch in fname if ch in whitelist])
    split = stripped.split()
    result = " ".join([split[0], ''.join(split[1:])])
    return result
Then call it with:
good_sanitized = strip_filename(good_filename, whitelist)
bad_sanitized = strip_filename(bad_filename, whitelist)
print(good_sanitized)
# 'herearesomelettersand123numbers andonespace'
print(bad_sanitized)
# 'symbols andmorethanonespace'
