From 372fa87c552e6d0f56aacedfadb828775854e9c3 Mon Sep 17 00:00:00 2001 From: tiagorg Date: Mon, 7 Nov 2022 17:21:08 +0000 Subject: [PATCH] Added comparefiles.py --- 1ano/fp/aula06/comparefiles.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1ano/fp/aula06/comparefiles.py diff --git a/1ano/fp/aula06/comparefiles.py b/1ano/fp/aula06/comparefiles.py new file mode 100644 index 0000000..5ed2088 --- /dev/null +++ b/1ano/fp/aula06/comparefiles.py @@ -0,0 +1,29 @@ +from sys import argv + + +def main(): + if len(argv) != 3: + file1 = input('File 1: ') + file2 = input('File 2: ') + else: + file1, file2 = argv[1:] + + if compareFiles(file1, file2): + print('Os ficheiros são iguais!') + else: + print('Os ficheiros são diferentes!') + + +def compareFiles(file1, file2): + with open(file1, 'rb') as f1, open(file2, 'rb') as f2: + while True: + data1 = f1.read(1024) + data2 = f2.read(1024) + if data1 != data2: + return False + if not data1 or not data2: + return True + + +if __name__ == "__main__": + main()