简体   繁体   中英

[Python]: Double backslashes in command

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM