简体   繁体   English

通过 jupyter notebook 发出运行终端命令

[英]Issue running terminal commands through jupyter notebook

I am currently running a jupyter notebook from a github repo.我目前正在从 github 存储库运行一个jupyter 笔记本 One chunk goes like this:一大块是这样的:

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

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

The variables target and path were defined and no errors were raised.变量目标和路径已定义并且没有引发错误。

When the OpticalFlowGen.py file is run on the terminal with python OpticalFlowGen.py ---type 'Train' -- file 'data/video.mp4' , a popup appears and closes after the video file is processed by openCV and.jpg files will be saved in the system.在终端上运行OpticalFlowGen.py文件, python OpticalFlowGen.py ---type 'Train' -- file 'data/video.mp4' ,视频文件经过openCV和.jpg处理后弹出并关闭文件将保存在系统中。 However, when this command is run on the jupyter notebook, nothing pops up and no files are saved.但是,当在 jupyter notebook 上运行此命令时,不会弹出任何内容,也不会保存任何文件。 You can access this.py file from the same repository here .您可以在此处从同一存储库访问 this.py 文件。

Currently I have to run manually on the terminal file by file so save all the image output before I can run the notebook without error.目前,我必须逐个文件在终端上手动运行,所以在我可以运行笔记本之前保存所有图像 output 没有错误。 However, it will become an issue when I have too many video files, not using the for loop will be too cumbersome.但是,当我有太多的视频文件时,它会成为一个问题,不使用for循环会太麻烦。 Any idea on how to solve this issue?关于如何解决这个问题的任何想法?

In a Jupyter Notebook you don't have to use os.system , instead try to use !在 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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM