【发布时间】: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