What you write to a file is in fact "bytes". Python 2 or 3 (*Just that in Python2 it was str and we changed this to be more clear and explicit in Python 3 to bytes).
So:
with open("file.ext", "w") as f:
f.write(b"some bytes")
Example:
bash-4.3$ python
Python 2.7.6 (default, Apr 28 2014, 00:50:45)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("file.ext", "w") as f:
... f.write(b"some bytes")
...
>>>
bash-4.3$ cat file.ext
some bytesbash-4.3$
Normally you would use an encoding if you are dealing with Unicode strings (str in Python 3, unicode in Python 2). e.g:
s = "Hello World!"
with open("file.ext", "w") as f:
f.write(s.encode("utf-8"))
Note: As mentioned in the comments; open(filename, "wb") doesn't really do what you think it does - it just affects how newlines are treated.
wandwbis how newlines are translated.