python - 通过 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循环会太麻烦。关于如何解决这个问题的任何想法?
解决方案
在 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}
推荐阅读
- c# - Asp.Net Core - 在响应返回后需要服务时如何处理服务生命周期?
- python - 在dict中查找基于键的值长度
- r - 为什么我的前导函数会生成 NA 值?
- junit4 - 如何从 junit 测试生成 spring restdocs 和 Pact.io 协议?
- c++ - 指向成员类型和模板的指针
- recaptcha - 可以使用 api 创建 google recaptcha 站点和密钥吗
- class - 八度类成员变量更改不起作用
- laravel - Laravel 监听 socket.io 客户端发出
- image - 为什么图像未显示在 stackoverflow.com 和“i.stack.imgur.com”错误中
- android - ViewPager with FragmentPagerAdapter 不显示内容