[Python]: двойная обратная косая черта в команде - PullRequest
0 голосов
/ 11 апреля 2011

РЕЗЮМЕ:

Я хочу выполнить команду pskill.exe в python. Пробовал в дос-подсказке: C: \ Users \ JohanSA \ Documents \ EclipseWorkspace \ ProdataATFBASE \ src \ resources \ pskill.exe \ localhost -t 11780 -> Работай хорошо!

==> Но я хочу запустить ту же команду через Python.

prodataOSLib.executeCmd ("pskill.exe -t 11780 ", правда, правда)

-> Работайте хорошо!

Теперь я хочу добавить имя компьютера в команду pskill

prodataOSLib.executeCmd ( "pskill.exe \\ localhost -t 11780 ", True, True)

-> ЧТО НЕ РАБОТАЕТ

Вход в функцию executeCmd говорит:

11-04-2011 13:35:15 [prodataoslib-executeCmd] - ОТЛАДКА: Выполнение команды: C: \ Users \ JohanSA \ Documents \ EclipseWorkspace \ ProdataATFBASE \ SRC \ ресурсы \ pskill.exe \ localhost -t 11780

-> Проблема в том, что перед словом «localhost» есть только 1 обратная косая черта -> но как я могу получить эти 2 обратной косой черты перед «localhost»?

Может кто-нибудь помочь мне с этим?

Большое спасибо! Johan

Это функция executeCmd:

def executeCmd (self,command, useResourceFolder=False, resultlogging=False):
    ''' Execute a command (default from current execution-folder) like you should type it in a DOS-prompt.
    Parameters:
    command = the command to be executed
    useResourceFolder = False (=default) or True --> set True if you want to execute a file from the resourcefolder.
    resultlogging = True when you want logging the result.
    return: List with result-messages or error-messages
    '''
    if useResourceFolder:
        command = os.path.abspath(os.path.join(self.currentdir, "..", "resources",command)) # Set path from resource directory            
    try:
        self.logger.debug("Executing command: %s" % command)
        output = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        resultfile = output.stdout
        resultmessageList = []
        for i in resultfile.readlines():
            resultmessageList.append(i)  
        if resultlogging:
            if len(resultmessageList) > 0:
                resultstring=""
                for messageline in resultmessageList:
                    resultstring = resultstring + messageline.replace('\n', '')
                self.logger.debug("RESULT MESSAGE: %s " % resultstring)
        return resultmessageList
    except OSError, e:
        self.logger.error("Execution FAILED. Exception: %s" %(e))

1 Ответ

1 голос
/ 11 апреля 2011

Сделайте вашу строку как необработанную строку с префиксом r.Я не могу понять код и описание, но то, что вы ищете, называется raw_string, и оно получается с префиксом r.Как это r'c:\localhost\nyprogram'

...