[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“,对,对) -->干得好 现在我想把computername添加到pskill突击队 prodataOSLib.executeCmd(“pskill.exe \\本地主机-t 11780“,对,对) -->这不起作用 登录executeCmd函数时会显示: 11-04-2011 13:35:15 [prodataoslib executeCmd]-调试: 正在执行命令: C:\Users\JohanSA\Documents\EclipseWorkspace\ProdataATFBASE\src\resources\pskill.exe \本地主机-t 11780 -->问题是在“localhost”-->这个词之前只有一个反斜杠,但是我怎么能在“localhost”之前有这两个反斜杠呢 有人能帮我吗 非常感谢! 约翰 这是executeCmd函数:[Python]:命令中的双反斜杠 总结:,python,escaping,backslash,Python,Escaping,Backslash,我想在python中执行pskill.exe命令。 在dos提示符下尝试: C:\Users\JohanSA\Documents\EclipseWorkspace\ProdataATFBASE\src\resources\pskill.exe\localhost-t 11780 -->干得好 ==>但是我想通过Python运行相同的命令 prodataOSLib.executeCmd(“pskill.exe-t 11780“,对,对) -->干得好 现在我想把computername添加到psk
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))
通过在
r
前面加前缀,使字符串成为原始字符串。我无法理解代码和描述,但您要查找的是原始字符串,它是通过前缀r
获得的。像这样,r'c:\localhost\nyprogram'
我可以给出的可能原因是日志库正在写入日志文件,而解释器会显示它(而不是打印它)。使用原始字符串a时,一个反斜杠就足够了。