Home > python > Reindent a source file

Reindent a source file

Problem

You have a source file that you want to work with but it mixes tabs and spaces. The interpreter eats the file but it’s a nightmare to work with. How to correct it?

Solution

Fortunately, the standard installation comes with a handy utility called “reindent.py“. You can find it with “locate reindent.py“.

Usage:

reindent.py bad.py

It will make a backup (bad.py.bak) of the original file and overwrite bad.py. The new bad.py will be indented correctly with 4 spaces everywhere.

Update (20120814)
If you don’t find reindent.py, it is located here: /usr/share/doc/python2.7/examples/Tools/scripts/reindent.py.

You might have to install the package “python-examples” to have it.

About these ads
Categories: python Tags: , , ,
  1. nivir
    April 8, 2013 at 14:21

    Hi
    reindent.py is not in my scripts folder even though I’m using Python 2.7, Mac OS. In that case do I need to install reindent.py ?

    Thanks.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: