It's writing the backspace character, you're just not seeing it in whatever external program you're using.
Confirmation:
>>> with open('temptest.txt', 'w') as f:
... f.write('123\b456')
...
>>> with open('temptest.txt', 'r') as f2:
... s = f2.read()
...
>>> s
'123\x08456'
Try a tool like hexdump
to look at your file and see the raw bytes. Most text editors don't display \b
(U+0008, "backspace") in any visible way.
If you're looking to actually put a raw backslash followed by a raw b
, on the other hand, you need to escape the backslash: Python interprets \b
as meaning U+0008, just like how \n
means a newline and \t
means a tab.
The easiest way is to use a raw string:
my_content = r'''\b \b \b'''
Note the r
(for "raw") in front! This will have actual raw backslashes followed by actual raw b
s.
You can also double the backslash:
my_content = '''\\b \\b \\b'''
This has the same effect.