6

Possible Duplicate:
Tool to convert python indentation from spaces to tabs?

I have a number of python files (>1000) that need to be reformatted so indentation is done only with tabs (yes, i know PEP-8, but this is a coding standard of a company). What is the easiest way to do so? Maybe some script in Python that will os.walk over files and do some magic? I can't just grep file content since file can be malformed (mixed tab and spaces, different amount of spaces) but Python will still run it and i get it back working after conversion.

5
  • 1
    Aaaand how are you going to cope with significant space characters at the start of lines, such as within literal """ blocks?
    – Spacedman
    Mar 15, 2012 at 18:20
  • 1
    @Space I'm writing to stackoverflow.com exactly for this purpose: task is not so easy as it looks, so maybe someone already stumbled upon it and has working solution :).
    – grigoryvp
    Mar 15, 2012 at 18:22
  • google for pindent and use it as starting point. Mar 15, 2012 at 18:26
  • Then your company's coding standards for python are substantially broken. Using tabs causes problems.
    – Marcin
    Mar 15, 2012 at 18:26
  • 1
    +1, why the close votes? The possible duplicate has an accepted answer that pretty much just says "don't do that", but the OP said it's his company's standard already. @EyeofHell: the retab answers in that linked question are actually more useful. You can actually use vim in batch mode using vim +commands. Mar 15, 2012 at 18:33

2 Answers 2

6

I would suggest using this Reindent script on PyPI to convert all of your horribly inconsistent files to a consistent PEP-8 (4-space indents) version.

At this point try one more time to convince whoever decided on tabs that the company coding standard is stupid and PEP-8 style should be used, if this fails then you could use sed (as in hc_'s answer) or create a Python script to replace 4 spaces with a single tab at the beginning of every line.

3
  • 1
    Is it possible to use reindent on Windows? I installed it via ActivePython pip as pip install reindent and it seems to install fine, but trying to execute it in console shows 'not recognized as an internal or external command' error :(
    – grigoryvp
    Mar 15, 2012 at 18:36
  • 1
    I haven't tried it on Windows, but it looks like it is a standalone Python script so if you can find where pip installed it you should be able to run it as python C:\path\to\pip\install\reindent.py <args>. Mar 15, 2012 at 18:56
  • 1
    Why is aligning with tabs stupid? Using spaces seems to force many different people to look at the code in the same manner, which might be unconfortable if e.g. people use screens of different sizes. Oct 23, 2019 at 15:07
3

How about

find . -type f -iname \*.py -print0 | xargs -0 sed -i 's/^    /\t/'

This command finds all .py files below the current directory and replaces every four consecutive spaces it finds inside of them with a tab.

Just noticed Spacedman's comment. This approach will not handle spaces at the beginning of a line inside a """ string correctly.

Not the answer you're looking for? Browse other questions tagged or ask your own question.