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~
Related
Im trying to write a script:
env PYTHONPATH=$PYTHONPATH: $Dir/scripts find * -name ‘*.py' -exec pylint (} \\; | grep . && exit 1
The code is finding all scripts in the root directory instead of using the environmental variables I set. Any help on writing this code to only look for files in the directory I set as a value in PYTHONPATH.
env PYTHONPATH=$PYTHONPATH: $Dir/scripts isn't doing what you think it's doing. Including $PYTHONPATH includes the former value of PYTHONPATH, meaning whatever you have it already set to or a blank default. The space in your variable also makes it invalid, and instead interprets the $Dir/scripts as a new command. It looks like what you want would be env PYTHONPATH=$Dir/scripts — but there's actually an easier way.
If you have __init__.py files in your directory, you can just do pylint ./some-directory. If you don't, you can use xargs: find . -type f -name "*.py" | xargs pylint. If you wanted to pass the directory instead of have it coded to . (your current calling directory) you could do that too:
# set directory to first argument
dir="$1"
# check if "dir" was actually provided, if not, set to `.`
if [ -z "$dir" ]; then dir=.; fi
find "$dir" -type f -name "*.py" | xargs pylint
You could save that in a script or function and then call it either with a directory (like run-pylint-on-everything.sh ~/foo/bar, or not, in which case it would run starting from your current shell location.
There’s no space between the PYTHONPATH value, it was a typo mistake, I want to run the command on a CLI instead of a script.
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 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.
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