#python trailing whitespace 제거 스크립트

less than 1 minute read

trailing whitespace 제거를 자동화하려고 만들었다. jenkins에 물릴 생각이다. 내 첫 파이썬 스크립트.

def iterfiles(rootdir, ft):
    for rt, _, files in os.walk(rootdir):
        for file in files:
            if ft(file):
                yield os.path.join(rt, file)

generator로 편하게 iterable 객체를 만들 수 있다. 상용구 코드(boilerplate code)도 없고 깔끔.

with open(fname, 'w') as f:
    f.writelines('\n'.join(clean_lines))
    f.write('\n') # newline at end of file

newline으로 끝낸다.

A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.

Since this is a “shall” clause, we must emit a diagnostic message for a violation of this rule.

This is in section 2.1.1.2 of the ANSI C 1989 standard. Section 5.1.1.2 of the ISO C 1999 standard (and probably also the ISO C 1990 standard).

why does GCC warn about “no newline at end of file”?

git diff 볼 때, No newline at end of file 메시지가 궁금해서 찾아보니 역사적인 이유 때문이었다. 보기 싫은 경고 메시지니 그냥 관례를 지킨다.

bin/remove-trailing-whitespace 스크립트