Replace Non-Breaking Spaces

Below Python 3 code replaces non-breaking-space characters with regular spaces for all filenames in the curent directory.

import os

for fileName in os.listdir():
	newFileName = fileName.replace("\xa0", " ")
	os.rename(fileName, newFileName)

What is non-breaking space

Non-breaking space is a space character that prevents automatic line break. It looks identical to a normal space character. By opening an UTF-8 encoded text file in binary editor, one can check that non-breaking space is 0xC2 0xA0 while normal space is 0x20. In Unicode, non-breaking space is represented as U+00A0 and this is what "\xa0" means in the above code.

In HTML, non-breaking space can be inserted with HTML entity  . When a user renames a file with a piece of text directly copied from a website, one can end up with a filename that contains non-breaking spaces.

If a filename contains non-breaking spaces, and the user tries to zip that file, Windows 10 shows an error:

Compressed (zipped) Folders Error ...filename... cannot be compressed because it includes characters that cannot be used in a command folder, such as . You should rename this file or directory.

Since non-breaking spaces are indistinguishable from regular spaces just by looking at them, one cannot identify which spaces are causing the error. He/she has to re-type all the spaces and this is a very time-consuming task if there are many files.