Substring functions

dnedrow at usa.net dnedrow at usa.net
Fri Feb 2 22:30:47 CET 2001


I'm trying to fixup someone elses work and am a complete Python neophyte.

The are two functions (is that even the term?) as follows:

def shellProtect(file):
  """Protects a filename against shell interpretation.
  This is done by putting the name in single quotes, and by
  escaping single quotes in the filename.
  """
  return "'%s'" % string.replace(file, "'", "'\\''")


def mv(self, src, dest):
  """Shorthand for the pattern trace('mv...);os.system("mv...)."""
  self.system("mv %s %s" % (shellProtect(src), shellProtect(dest)))

Basically, mv() is called with source and destination parameters. These
are quoted via shellProtext() and then passed as input parameters for the
Unix mv command.

E.g. if src=/usr/tmp/1/* and dest=/usr/tmp/2, the mv command issued by
calling mv() above would be:

  mv '/usr/tmp/1/*' '/usr/tmp/2'

Unfortunately, a wildcard doesn't work when quoted as above. How do I
check for an * at the end of the string and quote the input as
  '/usr/tmp/1/'*
rather than
  '/usr/tmp/1/*'

Any pointers would be appreciated.

-David


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list