[Python]:命令中的双反斜杠
摘要:
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加前缀
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 prefixingr
. Like thisr'c:\localhost\nyprogram'