how to apply yapf to every python file under a directory?
Asked Answered
C

1

14

I have installed Google yapf (yet another python formatter) under a project and try to format all my python files in-place, however I got the following error:

$ yapf -i -r files **.py
yapf: Input filenames did not match any python files

why yapf is incapable of understanding pattern? What should I do to achieve the same thing?

EDIT I also tried yapf -ir as suggested but I got:

$ yapf -ir
usage: yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
            [--style STYLE] [--style-help] [--no-local-style] [-p] [-vv]
            [files [files ...]]
yapf: error: cannot use --in-place or --diff flags when reading from stdin

which is strange as I'm not reading from stdin

Customs answered 18/11, 2018 at 3:2 Comment(1)
That's not what I suggested...Guillemot
G
29

The first problem is that wildcard expansion happens in the shell, before the command line even executes. When you run:

somecommand *.py

That command doesn't know you typed a *. All it knows is that you passed in a list of files. In other words, yapf is incapable of understanding the pattern because it never sees the pattern.

The second problem is that ** isn't a valid shell file globbing pattern. That's semantically equivalent to *, so running yapf -ir files **.py will only process any .py files contained in your current directory and inside the files directory.

If you want to run yapf recursively on all your Python files, starting in your current directory, there are a few solutions. The simplest is probably:

yapf -ir .

This will process all .py files in your current directory and its children. If you want more control over the selection of files, use find and xargs:

find . -name '*.py' -print0 | xargs -0 yapf -i
Guillemot answered 18/11, 2018 at 4:21 Comment(2)
thanks a lot, the second command works properly, the first one doesn't, as I have shown in my question update.Customs
You forgot the . in the command in your example. Since you didn't pass yapf a list of files, it thinks you're trying to process Python input on stdin.Guillemot

© 2022 - 2024 — McMap. All rights reserved.