【问题标题】:Python how to store "\" inside an arrey to use in a subprocess.call bash commandPython如何将“\”存储在数组中以在subprocess.call bash命令中使用
【发布时间】:2017-11-14 14:21:51
【问题描述】:

我有一个 python 程序,它创建一个 bash 命令,然后在 subprocess.call(command) 中使用它

我想给它发送一个带空格的文件位置。

import subprocess
command = ["mkdir","/home/matt/Desktop/this"]
command[1] +=  "\\ is"
subprocess.call(command)

但是当它被子进程使用时,它会变成一个名为的新目录 这\是

这是完整的代码

import pyxhook
import subprocess

command = [""]
isActive = False
element = 0
isSpace = False

def OnKeyPress(event):
    global command
    global isActive
    global element
    global isSpace
    if event.Ascii == 96:
        if isActive == False:
            isActive = True
        elif isActive == True:
            subprocess.call(command)
            command = [""]
            element = 0
            isActive = False
    elif isActive == True:
        if event.Ascii == 32:
            if isSpace == False:
                command.append("")
                element += 1
            else:
                command[element] += " "
                isSpace = False
        elif event.Key == "BackSpace":
            command[element] = command[element][:-1]
        elif event.Key == "slash":
            command[element] += "/"
        elif event.Key == "Shift_L" :
            command = command
        elif event.Key =="Shift_R":
            command = command
        elif event.Key == "backslash":
            isSpace = True
            command[element] +=  "\\"
        else:
            command[element] += (event.Key)

#instantiate HookManager class
new_hook=pyxhook.HookManager()
#listen to all keystrokes
new_hook.KeyDown=OnKeyPress
#hook the keyboard
new_hook.HookKeyboard()
#start the session
new_hook.start()

【问题讨论】:

  • @smarx 它将运行。字符串将被隐式转换为字符列表。然后它将与列表一起扩展。
  • 我编辑了我的帖子以反映这些问题并发布了我的完整代码
  • 你不需要转义任何东西,因为 command 没有被 shell 处理。字符串值作为一个参数直接传递给execve(或execs 之一)。

标签: python bash subprocess


【解决方案1】:

保持简单:

path = "/home/matt/Desktop/this"
path += " is"
os.makedirs(path, exist_ok=True)

如果您的 Python 版本早于 3.2,请删除 exist_ok=True

【讨论】:

    【解决方案2】:

    注意你的命令列表以及你是如何添加东西的:

    $ python
    Python 2.7.10 (default, Feb  7 2017, 00:08:15)
    [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess
    >>> command = ["mkdir", "/home/matt/Desktop/this"]
    >>> command[0] +=  "\\ is"
    >>> command
    ['mkdir\\ is', '/home/matt/Desktop/this']
    >>> command = ["mkdir", "/home/matt/Desktop/this"]
    >>> command[-1]
    '/home/matt/Desktop/this'
    >>> command[-1] += " is"
    >>> command
    ['mkdir', '/home/matt/Desktop/this is']
    >>> subprocess.call(command)
    0
    >>> import glob
    >>> glob.glob("/home/matt/Desktop/th*")
    ['/home/matt/Desktop/this is']
    >>>
    

    【讨论】:

      【解决方案3】:

      感谢您的帮助

      import subprocess
      command = ["mkdir","/home/matt/Desktop/this"]
      command[1] +=  " is"
      subprocess.call(command)
      

      使用子进程时不需要使用\

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-14
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 2011-02-13
        相关资源
        最近更新 更多