子进程“没有这样的文件或目录”

时间:2021-03-13 18:53:48

标签: python command-line subprocess

我做错了什么?我有一个可以在命令行中运行的命令,该命令可以查找有效目录中所有视频文件的总持续时间。

find /Volumes/Storage/test -maxdepth 1 -iname '*.mp4' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc

它工作正常,输出秒数 > 456.766000,这正是我想要的。

现在,当我编写 python 脚本时,它说 No such file or directory 并且不输出命令行命令最终执行的数字行。

代码

import subprocess

result2 = subprocess.run(['find', '/Volumes/Storage/test -maxdepth 1 -iname \'*.mp4\' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc'])
print (result2)
print ("-------")

错误:

No such file or directory

find: /Volumes/Storage/test -maxdepth 1 -iname '*.mp4' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc: No such file or directory
CompletedProcess(args=['find', "/Volumes/Storage/test -maxdepth 1 -iname '*.mp4' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \\; | paste -sd+ -| bc"], returncode=1)

Shlex 输出

['find', '/Volumes/Storage/test', '-maxdepth', '1', '-iname', '*.mp4', '-exec', 'ffprobe', '-v', 'quiet', '-of', 'csv=p=0', '-show_entries', 'format=duration', '{}', ';', '|', 'paste', '-sd+', '-|', 'bc']

1 个答案:

答案 0 :(得分:0)

这是对我有用的解决方案:

# Python program to explain os.system() method 
    
# importing os module 
import os 

# Command to execute 
cmd = 'find /Volumes/Storage/test -maxdepth 1 -iname \'*.mp4\' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc'

# Using os.system() method 
out = os.popen(cmd).read()

print("Output:",out)
相关问题