-1

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))
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
ITBudget
  • 1
  • 2

1 Answers1

1

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'

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • The probable reason I can give, is the logging library is writing to the log file as the interpreter would display it (as opposed to print it). http://stackoverflow.com/questions/2179493/adding-backslashes-without-escaping-python When you are using raw string a, a single backslash is enough. – Senthil Kumaran Apr 11 '11 at 12:22