View carriage returns, line feeds and tabs within text files

This is a useful Python one-liner for viewing characters that are usually hidden in text files;

  • Carriage returns (shown as \r)
  • Line feeds (shown as \n)
  • Tabs (shown as \t)
python -c "f = open('filename.txt', 'r'); f.seek(0); file = f.readlines(); print file"

Example output:

['Carriage return and line feed: \r\n', 'Line feed: \n', 'Tab: \t\n']

I recently used this script when trying to work out why a shell script was not executing correctly on a Debian machine — the reason was some carriage returns inserted by a Windows-based editor.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.