pythonlinux

Python: Spaces to Tabs?


I have a python file (/home/test.py) that has a mixture of spaces and tabs in it.

Is there a programmatic way (note: programmatic, NOT using an editor) to convert this file to use only tabs? (meaning, replace any existing 4-spaces occurrences with a single tab)?

Would be grateful for either a python code sample or a linux command to do the above. Thanks.


Solution

  • Sounds like a task for sed:

    sed -e 's/    /\t/g' test.py > test.new
    

    [Put a real tab instead of \t]

    However...

    Use 4 spaces per indentation level.

    --PEP 8 -- Style Guide for Python Code