Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write python file with various asci codes

I would like to write a python file containing four bytes:

  • A space
  • A tab
  • A carriage return
  • A newline

I’m not able to write it as:

open('file.txt', 'w').write(' \t\r\n')

As for whatever reason it’s converting the \r into a \n. How would I then write this as the asci codes themselves? that is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NEWLINE = 10
CARRIAGE = 13
SPACE = 32
TAB = 9
open('file.txt','wb').write(bytearray([SPACE, TAB, CARRIAGE, NEWLINE]))

>Solution :

Open the file in binary mode and write a byte string.

open('file.txt', 'wb').write(b' \t\r\n')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading