【发布时间】:2023-03-24 07:27:01
【问题描述】:
出于某种原因,无论我尝试了多少变体,我似乎都无法执行我编写的 bash 脚本。命令字在终端中 100% 没问题,但是当我尝试使用子进程调用它时,它什么也不返回。
from os import listdir
import subprocess
computer_name = 'homedirectoryname'
moviefolder = '/Users/{}/Documents/Programming/Voicer/Movies'.format(computer_name)
string = 'The lion king'
for i in listdir(moviefolder):
title = i.split('.')
formatted_title = title[0].replace(' ', '\ ')
if string.lower() == title[0].lower():
command = 'vlc {}/{}.{}'.format(moviefolder, formatted_title, title[1])
subprocess.call(["/usr/local/bin",'-i','-c', command], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
else:
continue
bash 可执行文件如下所示:
#/bin/bash
func() {
open -a /Applications/VLC.app/Contents/MacOS/VLC $1
}
我哪里出错了?
【问题讨论】:
-
不确定问题,但
subprocess.call文档说不要将PIPE与stdout和stderr一起使用 -
@MoinuddinQuadri 如果我把它们拿出来,它会在终端返回
-i: /usr/local/bin: is a directory -
你为什么还要尝试调用一个shell? shell 是一个位于操作系统之上的实用程序,它为您提供一个用户界面来启动程序。你不需要那个,只需启动 VLC。
-
使用check_call,这样你就可以看到你的调用是否成功。 call 不检查任何内容。
-
错误信息正确:
/usr/local/bin是目录。您不希望能够将其作为命令运行。
标签: python bash subprocess