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
No need for subprocess or format. Something as simple as:
for idx in range(10):
!python process.py --filename /Users/images/{idx}.jpg
works for me.
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)}
The !
simply indicates that the following code will be executed in the terminal.
So one option is just to code your statement in bash. It's not quite as easy as Python, but you can accomplish the same task like this:
! for file in /Users/images/*.jpg; do python process.py --filename /Users/images/$i; done
It's a for loop, but not a Python for loop.
Alternatively, consider going back to the source code of process.py
and modifying it so that it will loop through the files in a directory. This can be done easily enough with the os.listdir
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With