How can I pass filenames with spaces as arguments?

Don't parse ls. Just use:

python script.py /path/to/*.jpg

This performs shell globbing which replaces /path/to/*.jpg by the proper list.


I think the glob answer above is best, but xargs and find is also a solution that can be used sometimes.

find /some/dir/ -name '*.jpg' -print0 | xargs -0 python script.py

This works because -print0 on find will separate the output with null bytes rather than spaces, and the -0 on the xargs command line will assume the input is separated by null bytes.