|
def fast_num_lines(path: str) -> int: |
|
# See https://stackoverflow.com/q/9629179/388951 for the idea to use a Unix command. |
|
# Unfortunately `wc -l` ignores the last line if there is no trailing newline. So |
|
# instead, see https://stackoverflow.com/a/38870057/388951 |
|
return int(subprocess.check_output(['grep', '-c', '', path])) |
grep -c returns an error code on empty files, which subprocess.check_output turns into an exception.
$ touch blank.txt
$ grep -c '' blank.txt
0
$ echo $?
1
The grep manpage say an exit code of 1 means "no match" while an exit code of 2+ means there was an error.
webdiff/webdiff/diff.py
Lines 41 to 45 in d8606b1
grep -creturns an error code on empty files, whichsubprocess.check_outputturns into an exception.The
grepmanpage say an exit code of 1 means "no match" while an exit code of 2+ means there was an error.