Created
May 6, 2019 19:37
-
-
Save sigmapie8/1e326229ef87321f061e27259cfd8879 to your computer and use it in GitHub Desktop.
searching_multiple_txt_files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
source_location = "<location\\of\\the\\folder\\where\\configuration\\files\\are\\kept" | |
#Note how \\ is used. It is neccessary that you use \\ instead of \ for window file paths | |
searchString = "sha512WithECDSAEncryption" | |
def hasEncryption(file, encryptionString): | |
# this functions opens a single file and checks | |
# if the file has searchString that we're looking for | |
with open(file, "r") as server_config_file: | |
# open function here, takes two arguments | |
# 1 - Filename - the file that we want to open | |
# 2 - Filemode - how do we want to open it | |
# r - read mode, means we can read the contents of the file, | |
# throws an error if file is not present | |
# w - write mode, means we can write into the file, | |
# if the file is present, it gets overwritten | |
# if the file is not present already, it gets created | |
# a - append mode, means we can edit the file and add things to it | |
# As write mode always overwrites the file, we require append | |
# mode to add something to already made files. | |
# If the file is not present already, new file gets created. | |
# please note how we assigned the name server_config_file to our newly | |
# opened file. Python now does not recognize the actual name of the file | |
# but only recognises the name we assigned to it - server_config_file | |
config_file_content = server_config_file.read() | |
# .read() function allows us to read the whole file at once. | |
# all the content inside the file is now available in | |
# config_file_content variable as a string. | |
# you can even print it, to see for yourself. | |
if(encryptionString in config_file_content): | |
# Here in keyword is as literal as in english | |
# We are checking if our searchString is 'in' (within) | |
# our string variable - config_file_content | |
# As all the content inside our file is now stored in the | |
# form of a single string inside config_file_content | |
# using 'in' keyword we check if there a string that's | |
# a substring of the string inside config_file_content | |
# eg. if("am" in "I am awesome") would return true because | |
# "am" is a substring of "I am awesome" | |
return True | |
# this return is the return value of the function | |
return False | |
# function returns false if | |
def log_unencrypted(file): | |
#This function just logs the files we are interested in | |
#inside a text file. As we haven't given any specific location | |
#for the txt file, it gets created in the same location as | |
#our python script | |
with open("unecrypted_files.txt", "a") as result_file: | |
# for explanation on open function, read hasEncrypted function | |
result_file.write(file+"\n") | |
# here we used the write function of the file object (result_file) | |
# it'd write anything provided inside .write(<here>) into the file. | |
# note that we add an extra \n for bringing the cursor to next line. | |
#os.walk allows us to iterate through all the files and folder in the | |
#given location | |
for i in os.walk(source_location): | |
path, dirs, files = i | |
for file in files: | |
if(not hasEncryption(path+"\\"+ file, searchString)): | |
#notice how we used path+file and not just file because | |
#we need full file path to locate a file | |
# Here not will invert any boolean result coming from hasEncryption | |
# function. So if the function returns True, the condition inside | |
# 'if' will become false and vice versa. | |
log_unencrypted(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment