def nt_quote_arg(arg):
 result = []
 needquote = False
 nb = 0
 needquote = ((' ' in arg) or (' DCTB ' in arg))
 if needquote:
  result.append('"')
 for c in arg:
  if (c == '\\'):
   nb += 1
  elif (c == '"'):
   result.append((('\\' * (nb * 2)) + '\\"'))
   nb = 0
  else:
   if nb:
    result.append(('\\' * nb))
    nb = 0
   result.append(c)
 if nb:
  result.append(('\\' * nb))
 if needquote:
  result.append(('\\' * nb))
  result.append('"')
 return ''.join(result)

def nt_quote_arg ( arg ) : result = [ ] needquote = False nb = 0 needquote = ( ( ' ' in arg ) or ( ' DCTB ' in arg ) ) if needquote : result . append ( '"' ) for c in arg : if ( c == '\\' ) : nb += 1 elif ( c == '"' ) : result . append ( ( ( '\\' * ( nb * 2 ) ) + '\\"' ) ) nb = 0 else : if nb : result . append ( ( '\\' * nb ) ) nb = 0 result . append ( c ) if nb : result . append ( ( '\\' * nb ) ) if needquote : result . append ( ( '\\' * nb ) ) result . append ( '"' ) return '' . join ( result )
quote a command line argument according to windows parsing rules .