Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子进程调用可以';在Mac OS X上找不到完整路径_Python_Macos_Subprocess - Fatal编程技术网

Python子进程调用可以';在Mac OS X上找不到完整路径

Python子进程调用可以';在Mac OS X上找不到完整路径,python,macos,subprocess,Python,Macos,Subprocess,在我的Python脚本中,这一行: call("/Applications/BitRock\\ InstallBuilder\\ for\\ Qt\\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh") 每次都会失败,出现错误OSError:[Errno 2]没有这样的文件或目录 但是,如果我写出该字符串的结果: sys.stdout.write("/Applications/BitRock\\ InstallBuilder\\ f

在我的Python脚本中,这一行:

call("/Applications/BitRock\\ InstallBuilder\\ for\\ Qt\\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh")
每次都会失败,出现错误
OSError:[Errno 2]没有这样的文件或目录

但是,如果我写出该字符串的结果:

sys.stdout.write("/Applications/BitRock\\ InstallBuilder\\ for\\ Qt\\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh")
我得到:

/Applications/BitRock\ InstallBuilder\ for\ Qt\ 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh
如果我把它直接放进终端,它工作得很好


我缺少什么?

默认情况下
子流程。call
不使用shell(
shell=False
)。因此,没有必要逃离空间。shell中需要进行空间转义(因为shell需要知道二进制文件的名称和参数)。因此,以下用途都是正确的(和类似的):

  • 不生成生成子流程的shell(有利):

  • 或者同样没有shell(显式
    shell=False
    和参数列表的使用)

  • 但是当
    子进程
    首先被告知swawnshell,然后生成子进程本身时,必须转义空格,因为这是一个shell命令:

    from subprocess import call
    call('/Applications/BitRock\\ InstallBuilder\\ for\\ Qt 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh',
         shell=True)
    
  • 另一种方法是使用shell和引号:

    from subprocess import call
    call('"/Applications/BitRock InstallBuilder for Qt 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh"',
         shell=True)
    
  • 我建议尽可能不要使用shell(主要是出于安全原因),记住,如果不使用shell,则必须将参数作为
    列表
    传递给命令

    要么(无外壳,有利):

    还是带壳

    call('/bin/echo foo bar', shell=True)
    

    (两个调用都有相同的输出(
    foo bar\n

    很好,谢谢,选项4工作得很好,引号和shell。安全性不是一个风险,因为它是一个只有我可以访问的简单构建机。但感谢所有信息!
    from subprocess import call
    call('"/Applications/BitRock InstallBuilder for Qt 8.5.2/bin/Builder.app/Contents/MacOS/installbuilder.sh"',
         shell=True)
    
    call(['/bin/echo', 'foo', 'bar'])
    
    call('/bin/echo foo bar', shell=True)