Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Spaces to Tabs?

Tags:

python

linux

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.

like image 614
user3262424 Avatar asked Jan 20 '23 02:01

user3262424


1 Answers

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

like image 89
Johnsyweb Avatar answered Jan 28 '23 22:01

Johnsyweb