Prechádzať zdrojové kódy

Ignore files that cannot be opened (e.g. sockets);minor fixes

dennisro 5 rokov pred
rodič
commit
03a7f54f2a
1 zmenil súbory, kde vykonal 12 pridanie a 11 odobranie
  1. 12 11
      2.py

+ 12 - 11
2.py

@@ -57,14 +57,17 @@ def joinDicts(dict1, dict2):
 
 
 def hashfile(path, blocksize = 65536):
-    afile = open(path, 'rb')
-    hasher = hashlib.sha256()
-    buf = afile.read(blocksize)
-    while len(buf) > 0:
-        hasher.update(buf)
+    try:
+        afile = open(path, 'rb')
+        hasher = hashlib.sha256()
         buf = afile.read(blocksize)
-    afile.close()
-    return hasher.hexdigest()
+        while len(buf) > 0:
+            hasher.update(buf)
+            buf = afile.read(blocksize)
+        afile.close()
+        return hasher.hexdigest()
+    except:
+        pass
 
 
 def printResults(dict1):
@@ -72,7 +75,6 @@ def printResults(dict1):
     for size in sizes:
         final[size] = []
     del size
-    print(final)
     if bytes > 0:
         final[bytes] = []
     results = list(filter(lambda x: len(x) > 1, dict1.values()))
@@ -96,7 +98,7 @@ def printResults(dict1):
                 final[sizes[5]].append(result)
             else:
                 final[sizes[0]].append(result)
-    if len(results) > 0:
+    if len(results) > 0 and not bytes:
         print('___________________')
         print('\n\033[1;34m\033[1;34m\u25b6 Duplicates Found\033[0m\n')
         print('  The following files are identical. The name could differ, but the content is identical')
@@ -124,8 +126,7 @@ def printResults(dict1):
                     print('___________________')
 
     else:
-        print('No duplicate files found.')
-
+        print('\n\033[1mNo duplicate files found.\033[0m')
 
 if __name__ == '__main__':
     if len(sys.argv) > 1: