In: Computer Science
You work for the Gotham Times and you wish discover if Bruce Wayne is really Batman. We have exfiltrated some files and you will use Python and file forensics to find the string "Batman" in one of the files to prove it once and for all.
First, ask the user for the ABSOLUTE path to Bruce Wayne's folder and save it to a string variable. Next, convert the string from the user to a Path variable. Third, you will list all files in the folder given to you by the user and display the name of each file to the user. Finally, you will open up each file and look for the string "Batman". If Batman is found, let the user know that we have found Batman and what file we found the string in.
# Import required modules
from pathlib import Path
import os
###############################################################
# Part 1: Ask the user for the ABSOLUTE path to Bruce Wayne's
# folder and save it to a string variable
###############################################################
###############################################################
# Part 2: Convert that string to a Path variable
###############################################################
###############################################################
# Part 3: List all files in the folder and display the name of
# each file to the user
###############################################################
###############################################################
# Part 4: Open up each file and look for the string "Batman".
# If Batman is found, let the user know that
# we have found Batman and what file we found the
# string in
#
# HINT: Here is an example of looking for a substring
# within a string varible (we'll call the variable
# fileContents) (from previous lessons)
#
# if "Batman" in fileContents:
# print("Found Batman!!")
###############################################################
OS
this module is a efficient way of using the operating system dependent functions. If you just want to read or write a file you can uses open(), if you want to play with systems paths or locations you can use os.path it is a very handy module which let you do file searching and many more OS operations
#importing a module which is important for the operating system activities
import os
from pathlib import Path
#getting input from user
str_path = input("enter the path to bruce wayn's folder and it should be correct")
#converting string to the operating system path (this step can be optional because os.listdir() function can take string values)
path = Path(str_path)
#getting all the files name from that directory / path
file_list=os.listdir(path)
#displaying every file name that is in that folder
print(file_list)
#traversing through each file and each line in that file through this loop
for files in file_list:
file=open(files,'r')
for line in file:
for word in line.split():
if word=="Batman":
print("found Batman!!")