Skip to content

Instantly share code, notes, and snippets.

@Lubba-64
Created March 21, 2022 14:49
Show Gist options
  • Save Lubba-64/310126bd0db69c751d92071d1a822b07 to your computer and use it in GitHub Desktop.
Save Lubba-64/310126bd0db69c751d92071d1a822b07 to your computer and use it in GitHub Desktop.
import os
# checks if a string contains another string
def ContainsString(str,checkfor):
for i in range(0,len(str)):
if (str[i:i+len(checkfor)] == checkfor):
return True
return False
# remove the directory if it contains a string
def PruneIfContains(str, opposite, files):
pruned_files = []
for name in files:
shouldprune = ContainsString(name,str)
shouldprune = (shouldprune if not opposite else not shouldprune)
if shouldprune:
pruned_files.append(name)
return pruned_files
# remove any one of the files in a list if it contains a string, or doesn't contain a string
def PruneIfContainsMultiple(list, files,_not = True):
pruned_files = files
for str in list:
pruned_files = PruneIfContains(str, _not, pruned_files)
return pruned_files
# get text from a file
def GetText(path):
with open(path,'rt') as file:
return str(file.readlines())
# print all elements in a list
def PrintAll(list):
for element in list:
print(element)
# print all contents of the files in a list
def PrintAllContents(list):
for element in list:
print(GetText(element))
# get all file contents in a list
def GetAllContents(list):
_return = ''
for element in list:
_return += GetText(element)
return _return
# get all cs file directories in a directory
def GetAllCSFiles(path):
list_of_files = []
for root, dirs, files in os.walk(path):
for file in files:
list_of_files.append(os.path.join(root,file))
list_of_files = PruneIfContainsMultiple(['.meta','.git','.png'], list_of_files)
list_of_files = PruneIfContainsMultiple(['.cs'],list_of_files,_not = False)
return list_of_files
# count the number of character occurences in a string
def CountNumberOfChars(str, charToCount):
count = 0
for char in str:
if (char == charToCount):
count += 1
return count
# count the number of string occurences in a string
def CountNumberOfStrings(str, stringToCount):
count = 0
for i in range(0,len(str)):
if (str[i:i+len(stringToCount)] == stringToCount):
count += 1
return count
# get the number of lines in each file for a given list of files
def GetLines(files):
contents = GetAllContents(files)
return CountNumberOfStrings(contents,r"\n")
# input, and return data
while (not input('should exit? ').lower() == 'yes'):
inputpath = input("Path Of Folder: ")
allCS = GetAllCSFiles(inputpath)
print(f'#OF Lines: {GetLines(allCS)}')
print(f'#OF Files: {len(allCS)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment