Skip to content
Advertisement

Running for loop terminal commands in Jupyter

I know how to run command line in Jupyter : using ! For example, run an image file 2.jpg on Python process.py

! python classify.py --filename /Users/images/2.jpg

Question is, how to process all files of a folder in iteration way (idx) in Jupyter cell, something like this:

for idx in range(10):
    ! python process.py --filename /Users/images/idx.jpg

Thanks

PS: I tried path , did not work:

for i in range(1,10):
    cur_path = '/Users/images/'+str(i)+'.jpg'
    path = os.path.expanduser(cur_path)
    print(i,path)
    ! python process.py --filename path

Advertisement

Answer

A possible hackish solution could be to use eval and let bash execute a string.

for idx in range(10):
    !eval {"python process.py --filename /Users/images/{image}.jpg".format(image=idx)}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement