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
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.
Related
I need to create a bash script that create individual zip file for each of python file in the same directory.
I found the following command could create the zip with the file and original file extension. e.g created test.py.zip for test.py
find . -name '*.py' -exec zip '{}.zip' '{}' \;
How can I update the command to get rid of the original file extension. e.g. test.zip instead of test.py.zip
Thanks in advance.
You can strip an extension using basename <file> <extension>. There are a variety of ways you could arrange to do that.
Using a loop in bash
For example, loop through the results from find and then zip the file:
for f in $(find . -name '*.py')
do
zip "$(basename "$f" .py).zip" "$f"
done
Using a subshell in find
Unfortunately we can't use $(...) in find ... -exec directly. However we can always invoke a shell and do it there:
find . -name '*.py' -exec sh -c 'zip "$(basename "$0" .py)".zip "$0"' '{}' \;
You can't do that with find's -exec option. You can use bash's while loop and sed to process find's output instead:
find . -name '*.py' -print | while read FILE; do zip `echo $FILE|sed 's/\py$//'`zip $FILE; done
I'm trying to find a way to change my master/child document workflow in LaTeX using the package "subfiles", so I'm trying to append and prepend to every file I already have the following lines:
Lines to prepend:
\documentclass[<mainfile>]{subfiles}
\begin{document}
Line to appen:
\end{document}
I was thinking of using bash but I could be nice too with Python, I don't know what would be best.
If you have any suggestion :-) ?
An intuitive way is to use cat
For example, first create two files prepend.tex and append.tex that contain the content you want to add. Concatenate them with the source file to create the desired target file
$ cat prepend.tex source.tex append.tex > target.tex
To apply it recursively to all existing files inside a directory say src/ you can use:
$ find src/ -type f -name "*.tex" -exec sh -c "cat prepend.tex '{}' append.tex > '{}'.tmp" \;
This will create a new .tmp file alongside each source file. Make sure that the result in those .tmp files are exaclty what you want before proceeding to overwrite the source files with the .tmp ones:
$ find src/ -type f -name "*.tmp" -exec rename -n 's/.tmp$//' '{}' \;
change the option from -n to -f of the rename command to force overwrite
I have a really weird problem. I'm developing a Pyramid project and it seems like non-existing module is found when I run pserve.
__init__.py of my main module:
...
# This works !!! db/models directory does not even exists
from db.models import Base
# Also works - db/model exists
from db.model import Base
...
I even tried to recreate my virtual environment and it still finds it.
Any ideas?
From the comments it appears this was solved and it was leftover *.pyc files. This problem can come up a lot if you're moving between branches or if you find yourself frequently renaming/deleting files.
$ find mydir -name "*.pyc" -exec rm {} \; will recursively find all *.pyc files in the "mydir" directory (replace mydir with your directory name of course) and delete them. $ find . -name "*.pyc" -exec rm {} \; for your current working directory.
If you're using git for your project, add this script to your post-checkout hook to prevent differences between branches from getting in your way.
$ echo "find src -name "*.pyc" -exec rm {} \;" >> .git/hooks/post-checkout
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!
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).
Use xargs:
find . -name "*.txt" | xargs python -c 'import sys; print sys.argv[1:]'
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 {} +
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
I have a Django project and I'm working on Pylinting my way through it.
I have a couple situations where I'd like to be able to recursively find all files with a given name and pylint them differently (using different options). For example, I'd like to set different options for pylinting urls.py and admin.py
The following works for 1 directory..
pylint ./project_name/*/urls.py
But I'd like to make that * recursive... so that it drills into sub directories.
Any way to achieve that?
Update
I'd also like them all to run as a single pylint output, not sequentially
Depending on your operating system, you could use:
find project_name -name urls.py | xargs pylint
Try with find:
find ./project_name/ -name "urls.py" -exec pylint '{}' \;
If you want to run multiple files in a single call to pylint:
find ./project_name/ -name "urls.py" -exec pylint '{}' +
Get all python files (recursive)
Pass them all at once to pylint (from answer by Robin)
Show output in console and put it in a file
find . -name "*.py" -print0 | xargs -0 pylint 2>&1 | tee err_pylint.rst~