방금 코드를 작성했습니다.
hasher = hashlib.sha1()
inputFile = open(inputPath, 'r')
hasher.update(inputFile.read().encode('utf-8'))
oldHash = hasher.hexdigest()
newHash = ''
while True:
hasher.update(inputFile.read().encode('utf-8'))
newHash = hasher.hexdigest()
if newHash != oldHash:
print('xd')
oldHash = newHash
신속하게 sass 컴파일러를 작성하고 사용자가 파일을 변경했는지 확인하는 방법을 작성해야합니다. 파일에 무언가를 추가 할 때만 작동하지만 단어 또는 문자를 삭제할 때 감지하지 못합니다.
왜 그런지 아십니까?
해시를 즉시 확인하는 대신을 사용하여 마지막 수정 시간을 확인할 수 있습니다 os.path.getmtime(path)
.
고려하면:
in_path = "" # The sass/scss input file
out_path = "" # The css output file
그런 다음 파일이 변경되었는지 확인하기 만하면됩니다.
if not os.path.exists(out_path) or os.path.getmtime(in_path) > os.path.getmtime(out_path):
print("Modified")
else:
print("Not Modified")
파일이 수정되었는지 확인한 후 해시를 확인할 수 있습니다.
import hashlib
def hash_file(filename, block_size=2**20):
md5 = hashlib.md5()
with open(filename, "rb") as f:
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.digest()
if not os.path.exists(out_path) or hash_file(in_path) != hash_file(out_path):
print("Modified")
else:
print("Not Modified")
전체적으로 다음과 같이 if 문을 결합 할 수 있습니다.
if not os.path.exists(out_path) \
or os.path.getmtime(in_path) > os.path.getmtime(out_path) \
or hash_file(in_path) != hash_file(out_path):
print("Modified")
else:
print("Not Modified")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다