filecmp — 파일과 디렉터리 비교

소스 코드: Lib/filecmp.py


filecmp 모듈은 다양한 선택적 시간/정확도 절충을 통해 파일과 디렉터리를 비교하는 함수를 정의합니다. 파일 비교에 대해서는, difflib 모듈을 참조하십시오.

filecmp 모듈은 다음 함수를 정의합니다:

filecmp.cmp(f1, f2, shallow=True)

f1f2로 이름이 지정된 파일을 비교하여, 같아 보이면 True를 반환하고, 그렇지 않으면 False를 반환합니다.

If shallow is true and the os.stat() signatures (file type, size, and modification time) of both files are identical, the files are taken to be equal.

Otherwise, the files are treated as different if their sizes or contents differ.

이 함수는 외부 프로그램을 호출하지 않으므로 이식성과 효율성을 제공합니다.

이 함수는 과거 비교와 결과에 대해 캐시를 사용합니다. 파일에 대한 os.stat() 정보가 변경되면 캐시 항목이 무효화 됩니다. 전체 캐시는 clear_cache()를 사용하여 지울 수 있습니다.

filecmp.cmpfiles(dir1, dir2, common, shallow=True)

두 디렉터리 dir1dir2에 있는 이름이 common으로 지정된 파일들을 비교합니다.

파일 이름의 세 가지 리스트를 반환합니다: match, mismatch, errors. match는 일치하는 파일 리스트를 포함하고, mismatch는 일치하지 않는 파일의 이름을 포함하며, errors는 비교할 수 없는 파일의 이름을 나열합니다. 파일이 디렉터리 중 하나에 없거나, 사용자가 읽을 수 있는 권한이 없거나, 다른 이유로 인해 비교를 수행할 수 없으면 파일은 errors에 나열됩니다.

shallow 매개 변수는 filecmp.cmp()와 같은 의미와 기본값을 가집니다.

예를 들어, cmpfiles('a', 'b', ['c', 'd/e'])a/cb/c, a/d/eb/d/e를 비교합니다. 'c''d/e'는 각각 반환된 세 개의 리스트 중 하나에 포함됩니다.

filecmp.clear_cache()

filecmp 캐시를 지웁니다. 파일이 수정된 후 너무 빨리 비교되어 하부 파일 시스템의 mtime 해상도 내에 있을 때 유용합니다.

버전 3.4에 추가.

dircmp 클래스

class filecmp.dircmp(a, b, ignore=None, hide=None)

Construct a new directory comparison object, to compare the directories a and b. ignore is a list of names to ignore, and defaults to filecmp.DEFAULT_IGNORES. hide is a list of names to hide, and defaults to [os.curdir, os.pardir].

dircmp 클래스는 filecmp.cmp()에서 설명한 대로 얕은(shallow) 비교를 수행하여 파일을 비교합니다.

dircmp 클래스는 다음 메서드를 제공합니다:

report()

ab 사이의 비교를 (sys.stdout로) 인쇄합니다.

report_partial_closure()

ab 및 공통 직접 하위 디렉터리 사이의 비교를 인쇄합니다.

report_full_closure()

ab 및 공통 하위 디렉터리 (재귀적으로) 사이의 비교를 인쇄합니다.

dircmp 클래스는 비교되는 디렉터리 트리에 대한 다양한 정보 비트를 얻는 데 사용될 수 있는 여러 가지 흥미로운 어트리뷰트를 제공합니다.

Note that via __getattr__() hooks, all attributes are computed lazily, so there is no speed penalty if only those attributes which are lightweight to compute are used.

left

디렉터리 a.

right

디렉터리 b.

left_list

hideignore로 필터링 된, a의 파일과 하위 디렉터리.

right_list

hideignore로 필터링 된, b의 파일과 하위 디렉터리.

common

ab의 공통 파일과 하위 디렉터리.

left_only

a에만 있는 파일과 하위 디렉터리.

right_only

b에만 있는 파일과 하위 디렉터리.

common_dirs

ab의 공통 하위 디렉터리.

common_files

ab의 공통 파일.

common_funny

ab의 공통 이름으로, 디렉터리 간에 유형이 다르거나, os.stat()가 에러를 보고하는 이름.

same_files

ab에 모두 있고, 클래스의 파일 비교 연산자를 사용할 때 같은 파일.

diff_files

ab에 모두 있고, 클래스의 파일 비교 연산자를 사용할 때 내용이 다른 파일.

funny_files

ab에 모두 있지만, 비교할 수 없는 파일.

subdirs

A dictionary mapping names in common_dirs to dircmp instances (or MyDirCmp instances if this instance is of type MyDirCmp, a subclass of dircmp).

버전 3.10에서 변경: Previously entries were always dircmp instances. Now entries are the same type as self, if self is a subclass of dircmp.

filecmp.DEFAULT_IGNORES

버전 3.4에 추가.

dircmp에 의해 기본적으로 무시되는 디렉터리 리스트.

다음은 이름이 같지만, 내용이 다른 파일을 표시하기 위해, subdirs 어트리뷰트로 두 개의 디렉터리를 재귀적으로 검색하는 간단한 예제입니다:

>>> from filecmp import dircmp
>>> def print_diff_files(dcmp):
...     for name in dcmp.diff_files:
...         print("diff_file %s found in %s and %s" % (name, dcmp.left,
...               dcmp.right))
...     for sub_dcmp in dcmp.subdirs.values():
...         print_diff_files(sub_dcmp)
...
>>> dcmp = dircmp('dir1', 'dir2') 
>>> print_diff_files(dcmp)