[Python]:命令中的双反斜杠

发布于 2024-10-31 07:24:20 字数 2320 浏览 4 评论 0原文

摘要:

我想在 python 中执行 pskill.exe 命令。 在dos提示符下尝试一下: C:\Users\JohanSA\Documents\EclipseWorkspace\ProdataATFBASE\src\resources\pskill.exe \localhost -t 11780 -->干得好!

==>但我想通过 Python 运行相同的命令。

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

--> 工作顺利!

现在我想将计算机名添加到 pskill commando

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\resources\pskill.exe \本地主机-t 11780

-->问题是“localhost”之前只有 1 个反斜杠 -->但是我怎样才能在“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))

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-11-07 07:24:20

通过添加前缀 r 将字符串设置为原始字符串。我无法理解代码和描述,但是您要查找的内容称为 raw_string,它是通过前缀 r 获得的。就像这样r'c:\localhost\nyprogram'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文