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
1 Answer
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 Comments
Avinash Raj
And it's better to add
-type f argument to the above find command.b_pcakes
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
But it doesn't work with piping just the script name to python
Avinash Raj
try
find -name foo.py -print | xargs python