3

I have a list of files that I can obtain using the UNIX 'find' command such as:

$ find . -name "*.txt"
foo/foo.txt
bar/bar.txt

How can I pass this output into a Python script like hello.py so I can parse it using Python's argparse library?

Thanks!

1
  • It is unclear what you mean to achieve. What do you want to do with the output? Why would you want to use argparse for this? Commented Sep 20, 2012 at 21:16

4 Answers 4

7

If you want just text output of find(1), then use a pipe:

~$ find . -name "*.txt" | python hello.py

If you are looking to pass list of files as arguments to the script, use xargs(1):

~$ find . -name "*.txt" -print0 | xargs -0 python hello.py

or use -exec option of find(1).

Sign up to request clarification or add additional context in comments.

Comments

7

Use xargs:

find . -name "*.txt" | xargs python -c 'import sys; print sys.argv[1:]'

Comments

5

From man find:

-exec command ;
      Execute  command;  true  if 0 status is returned.  All following
      arguments to find are taken to be arguments to the command until
      an  argument  consisting of `;' is encountered.  The string `{}'
      is replaced by the current file name being processed  everywhere
      it occurs in the arguments to the command, not just in arguments
      where it is alone, as in some versions of find.  Both  of  these
      constructions might need to be escaped (with a `\') or quoted to
      protect them from expansion by the shell.  See the EXAMPLES sec‐
      tion for examples of the use of the -exec option.  The specified
      command is run once for each matched file.  The command is  exe‐
      cuted  in  the starting directory.   There are unavoidable secu‐
      rity problems surrounding use of the -exec  action;  you  should
      use the -execdir option instead.

-exec command {} +
      This  variant  of the -exec action runs the specified command on
      the selected files, but the command line is built  by  appending
      each  selected file name at the end; the total number of invoca‐
      tions of the command will  be  much  less  than  the  number  of
      matched  files.   The command line is built in much the same way
      that xargs builds its command lines.  Only one instance of  `{}'
      is  allowed  within the command.  The command is executed in the
      starting directory.

So you can do

 find . -name "*.txt" -exec python myscript.py {} +

Comments

0

This helps, if you need to pass arguments after the list of arguments from the find output:

$ python hello.py `find . -name "*.txt"`

I used it to concat pdf files into another one:

$ pdfunite `find . -name "*.pdf" | sort` all.pdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.