Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Added word_detector to System Automation Scripts #109

Merged
merged 3 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions System-Automation-Scripts/Word detector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Word Detector

#### This script is used to detect whether a given word is present in a list of files.All the files which contain the specified word gets stored in a seperate folder.

#### It takes two inputs -

1.Path of the directory which contains the files.

[Sample path - "C:/Users/<Your System Name>/Desktop/<Target Folder>/"]

2.Word that you want to detect in these files.

[Sample word - "awesome"]

#### The output it produces -

A seperate folder in the current directory which contains the target files(files that contain the input word).
42 changes: 42 additions & 0 deletions System-Automation-Scripts/Word detector/word_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import shutil

# Inputs
directory_path = input("Enter the absolute path of the folder: ")
detect_this = input("Enter the word you want to detect in the files: ")

# Condition to omit creation of the folder if it already exists.
if f"Flagged_{detect_this}" not in os.listdir(directory_path):
os.mkdir(f"Flagged_{detect_this}")

# Creates a list of all the files/directories in the provided path
files = os.listdir(directory_path)

# To omit adding the python script and the folder in the list of files.
files.remove(f"Flagged_{detect_this}")
files.remove("detector.py")

# List to collect the files which contains the target word
flagged_files = []

# Logic to loop over the files and check for the presence of the target word in them and adding them to the flagged_files list.
for file in files:
with open(file, "r") as f:
if detect_this.lower() in f.read().lower():
flagged_files.append(file)


# Looping over the flagged files and moving them to a seperate folder
for file in flagged_files:
shutil.move(directory_path+file, directory_path +
"Flagged_"+detect_this)

# To make the output look organised
print(
f"The script was successfull in detecting {detect_this} in the list of files")
print(" ")
print("Summary of the detection: ")
print(f"Total Files: {len(files)}")
print(f"Files with the target word: {len(flagged_files)}")
print(" ")
print("Thanks for using this script..........")