whitespace within a string

Bart Nessux bart_nessux at hotmail.com
Mon Feb 23 20:22:25 EST 2004


Is there a function/module that can be used to throw out extra whitespace
that appears within a string? The problem that I have is this: Before any
configuration is done to my files, they have lines with tabs in between the
words like this:

"disable        =       yes"

After configuring the files using the operating system's administration
tools, the OS rewrites the files to contain spaces instead of tabs. So now,
the file looks like this:

"disable = yes"

I would like my script to work in either situation. Right now, it only works
with spaces, not tabs. Below is the script:

def enable_ssh():
   # works on 10.3 systems, not 10.2 systems
   import os
   os.chdir('/etc/xinetd.d')
   find = 'disable = yes'
   replace = 'disable = no'
   x = file('ssh')
   data = x.read()
   x.close()
   search = str.find(data, find)
   if search >= 0:
      data = data.replace(find, replace)
      outputFile = file('ssh', 'w')
      outputFile.write(data)
      outputFile.close()
      print
      print "SSH was disabled... service restarted!!!"
      print
   else:
      print
      print "SSH was enabled... no action taken."
      print
enable_ssh()



More information about the Python-list mailing list