Consider this simple Python command-line script:
"""foobar
Description
Usage:
foobar [options] <files>...
Arguments:
<files> List of files.
Options:
-h, --help Show help.
--version Show version.
"""
import docopt
args = docopt.docopt(__doc__)
print(args['<files>'])
And consider that I have the following files in a folder:
file1.pdffile 2.pdf
Now I want to pass the output of the find command to my simple command-line script. But when I try
foobar `find . -iname '*.pdf'`
I don't get the list of files that I wanted, because the input is split on spaces. I.e. I get:
['./file', '2.pdf', './file1.pdf']
How can I correctly do this?
This isn't a Python question. This is all about how the shell tokenizes command lines. Whitespace is used to separate command arguments, which is why
file 2.pdfis showing as as two separate arguments.You can combine
findandxargsto do what you want like this:The
-print0argument to find tells it to output filenames seperated by ASCII NUL characters rather than spaces, and the-0argument toxargstells it to expect that form of input.xargswith then call yourfoobarscript with the correct arguments.Compare:
To: