试试这个:
dosnames=['CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9']
final=''
string='th\xe8is i\xaas \xb2a te><s\x0b\xe6t\xb0.\xb4'
for char in string:
if not (char in '<>:"/\|?*'):
if ord(char)>31:
final+=char
if final in dosnames:
#oh dear...
raise SystemError('final string is a DOS name!')
elif final.replace('.', '')=='':
raise SystemError('final string is all periods!')
这将检查 dos 名称和不允许的 ascii 字符。然后,print final
给我'this is a test.'