From 2e195d44393fc7347e25c82f7e0f2144a18189d0 Mon Sep 17 00:00:00 2001 From: "Christopher K. Hoadley" Date: Tue, 31 Dec 2019 11:19:15 -0600 Subject: [PATCH] Move all writing of output files to occur after query takes place. Use with statement for results file, as that is more graceful on errors. Use try block for result directory creation: this has a smaller window for a race condition. --- sherlock/sherlock.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/sherlock/sherlock.py b/sherlock/sherlock.py index c3ac6a2..74e1b3e 100644 --- a/sherlock/sherlock.py +++ b/sherlock/sherlock.py @@ -599,17 +599,6 @@ def main(): for username in args.username: print() - if args.output: - file = open(args.output, "w", encoding="utf-8") - elif args.folderoutput: # In case we handle multiple usernames at a targetted folder. - # If the folder doesnt exist, create it first - if not os.path.isdir(args.folderoutput): - os.mkdir(args.folderoutput) - file = open(os.path.join(args.folderoutput, - username + ".txt"), "w", encoding="utf-8") - else: - file = open(username + ".txt", "w", encoding="utf-8") - results = sherlock(username, site_data, verbose=args.verbose, @@ -620,14 +609,28 @@ def main(): timeout=args.timeout, color=not args.no_color) - exists_counter = 0 - for website_name in results: - dictionary = results[website_name] - if dictionary.get("status").status == QueryStatus.CLAIMED: - exists_counter += 1 - file.write(dictionary["url_user"] + "\n") - file.write(f"Total Websites Username Detected On : {exists_counter}") - file.close() + if args.output: + result_file = args.output + elif args.folderoutput: + # The usernames results should be stored in a targeted folder. + # If the folder doesnt exist, create it first + try: + os.mkdir(args.folderoutput) + except FileExistsError: + #directory already exists + pass + result_file = os.path.join(args.folderoutput, f"{username}.txt") + else: + result_file = f"{username}.txt" + + with open(result_file, "w", encoding="utf-8") as file: + exists_counter = 0 + for website_name in results: + dictionary = results[website_name] + if dictionary.get("status").status == QueryStatus.CLAIMED: + exists_counter += 1 + file.write(dictionary["url_user"] + "\n") + file.write(f"Total Websites Username Detected On : {exists_counter}") if args.csv == True: with open(username + ".csv", "w", newline='', encoding="utf-8") as csv_report: