开发者

[Python]: Double backslashes in command [duplicate]

This question already has answers here: 开发者_如何学Python How can I put an actual backslash in a string literal (not use it for an escape sequence)? (4 answers) Closed 7 months ago.

SUMMARY:

I want to execute a pskill.exe command in python. Tried it in a dos-prompt: C:\Users\JohanSA\Documents\EclipseWorkspace\ProdataATFBASE\src\resources\pskill.exe \localhost -t 11780 --> work nice!

==> But I want to run the same command via Python.

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

--> Work nice!

Now I want to add the computername to the pskill commando

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

--> THAT DOESN'T WORK

Logging in the executeCmd-function says:

11-04-2011 13:35:15 [prodataoslib-executeCmd] - DEBUG: Executing command: C:\Users\JohanSA\Documents\EclipseWorkspace\ProdataATFBASE\src\resources\pskill.exe \localhost -t 11780

--> The problem is that there is only 1 backslash before the word "localhost" --> but how can i have these 2 backslashes before "localhost"?

Can anyone help me with this?

Many Thanks! Johan

This is the executeCmd function:

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))


Make your string as raw string by prefixing r. I am unable to understand the code and the description, but what are you looking for is called raw_string and it is obtained by prefixing r. Like this r'c:\localhost\nyprogram'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜