How to convert 2 spaces to 4 spaces "tabs" in python?


#1

I have 2 spaces and want to convert my python project/modules/files to 4 spaces. How can I do this?


#2

Have you tried find-and-replacing two spaces with four spaces?


#3

write program as

#!/usr/bin/env python

-- coding: iso-8859-15 --

##################################### look def helpMe
#####################################
##################################### Version: 27.12.12
##################################### Version: 30.05.13 name tabsToS

import sys, string, os

def helpMe () :
print ‘’’

############### tabsToS – Tabs to Spaces (change them in file 1 tab to 4 spaces)
############### calls: tabsToS.py source – in file source all Tabs change to Spaces (no destin file)
############### tabsToS.py source destin – file source copy to destin with changing of Tabs to Spaces
############### if destin is given as N or n destin file is created as sourcename_NEW.py
###############
############### remarks: source file should have extinction py

'''
return                         ##### helpMe #####

numArg = len (sys.argv) - 1
if numArg < 1 or numArg > 2 :
print ’ call tabsToS.py without argument or number of arguments > 3’
helpMe ()
sys.exit (1)

infilename = sys.argv[1] ### first argument
L = len (infilename)
if L < 4 :
print ’ len of infilename <%s> = %d < 4 ’ % (infilename, L)
sys.exit (1)

if infilename [-3 :] <> ‘.py’ :
print ’ extension of infilename <%s> <> .py ’ % infilename
sys.exit (1)

outfilename = infilename [: -3] + ‘_NEW.py’

if numArg > 1 :
st = sys.argv[2] ### second argument
L = len (st)
if L < 4 : ### N or ST
if st == ‘n’ or st == ‘N’ : ### create new destin
destEqSource = 0 ### destination file NOT equals source
else :
print ’ len of second argument <%s> = %d < 4 ’ % (st, L)
helpMe ()
sys.exit (1)
else : ### second argument is destin file
if st [-3 :] <> ‘.py’ :
print ’ extension of second argument <%s> <> .py ’ % st
helpMe ()
sys.exit (1)
else : ### we have destin name
outfilename = st
destEqSource = 0 ### destination file NOT equals source
else : ### no second argument
destEqSource = 1 ### destination file EQUALS source

p2 = os.getcwd () + ‘/’ + outfilename
if os.path.isfile (p2) : ### file exists
if destEqSource :
s1 = '\n tabsToS.py will use file %s as temporary ’ % outfilename
else :
s1 = '\n You have chosen destination file = %s ’ % outfilename

while 1 :
    print  s1 + '''
              BUT it exists !!!   Would you replace it?      
       
              INPUT one letter:  Y — yes  and tabsToS will continue job
                                 N — no   and tabsToS will not do job
                ''' 
    st = raw_input (' or one symbol for exit from rename : ')
    if  st == 'n'  or  st == 'N' :
        print ' THANK YOU == I am going to relax '
        sys.exit (1)     
    if  st == 'y'  or  st == 'Y' :
        break
    else :
        print ' You typed <%s>  Choose from y Y n N ' % st
        continue

try:
ifile = open (infilename, ‘r’) # open file for reading
except:
print ’ infilename = <%s> DOES NOT EXIST ’ % (os.getcwd () + ‘/’ + infilename)
sys.exit (1)

ofile = open (outfilename, ‘w’) # open file for writing
# file will be created or overwritten

nlines = 0 ### number of changed lines
LL = [] ### List of lines

chaTab = chr (9)

nl = 0 ### number of line
for line in ifile:
nl = nl + 1

if  string.find (line, chaTab) >= 0  :       ### found
    st = string.replace (line, chaTab, '    ')
    nlines  = nlines + 1
    ofile.write('%s' % st)  
    LL.append (nl)
    LLempty = 0
else :
    ofile.write('%s' % line)  

ifile.close()
ofile.close()

if destEqSource : ### delete infilename and rename *_NEW.py to *.py
if nlines == 0 : ### LL empty
print ’ ********************** NO TABS IN %s ’ % infilename
else :
p1 = os.getcwd () + ‘/’ + infilename
os.remove (p1)
os.rename (p2, p1)
print ’ *************************************** in file %s TABS changed to 4 Spaces ’ % infilename
else :
print ’ ********************* file %s with TABS changed to 4 Spaces copied in %s ’ % (infilename, outfilename)
if nlines == 0 : print ’ ************ BTW, file %s had not TABS ’ % infilename

BYE
aagorobets@gmail.com