0

I have a file in my directory called foo.py that contains Python code. How do I pipe the file to Python in the terminal so that Python will run it? Entering this into the terminal doesn't work:
find -name foo.py -print | python

0

1 Answer 1

1

Use -exec parameter to find and execute all the founded .py files.

find -name '*.py' -exec python {} \;

And for a single file, you may use

find -name 'foo.py' -exec python {} \;

Note that this would search for the name foo.py in the current directory as well as the subdirectories.

4
  • And it's better to add -type f argument to the above find command. Jul 21, 2015 at 2:31
  • This works, but I am wondering if there is a way to pipe it just out of curiosity, because it works if I pipe input to the script itself like so: echo <some argument> | python foo.py
    – b_pcakes
    Jul 21, 2015 at 3:41
  • But it doesn't work with piping just the script name to python
    – b_pcakes
    Jul 21, 2015 at 3:42
  • try find -name foo.py -print | xargs python Jul 21, 2015 at 6:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.