Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to auto convert spaces to tabs in multiple python files? [duplicate]

Tags:

python

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.

like image 794
grigoryvp Avatar asked Jan 18 '23 05:01

grigoryvp


1 Answers

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.

like image 54
Andrew Clark Avatar answered Apr 06 '23 00:04

Andrew Clark