首页 > 解决方案 > 通过 jupyter notebook 发出运行终端命令

问题描述

我目前正在从 github repo运行一个jupyter notebook 。一大块是这样的:

for file in os.listdir('data/'):
    path = 'data/' + file

    os.system(f"python OpticalFlowGen.py --type {target} --file {path}")

变量目标和路径已定义并且没有引发错误。

在终端上运行OpticalFlowGen.py文件时python OpticalFlowGen.py ---type 'Train' -- file 'data/video.mp4',会出现一个弹窗,视频文件经过openCV处理后关闭,系统会保存.jpg文件。但是,当在 jupyter notebook 上运行此命令时,不会弹出任何内容,也不会保存任何文件。您可以在此处从同一存储库访问此 .py 文件。

目前,我必须逐个文件在终端文件上手动运行,以便保存所有图像输出,然后才能运行笔记本而不会出错。但是,当我有太多的视频文件时,它会成为一个问题,不使用for循环会太麻烦。关于如何解决这个问题的任何想法?

标签: pythonopencvjupyter-notebook

解决方案


在 Jupyter Notebook 中,您不必使用os.system,而是尝试使用!

for file in os.listdir('data/'):
    path = 'data/' + file

    # Use
    # ! - for terminal commands
    # {} - for a variable in the terminal command
    !python OpticalFlowGen.py --type {target} --file {path}

推荐阅读