Skip to content

Instantly share code, notes, and snippets.

@Himan10
Last active June 14, 2019 15:54
Show Gist options
  • Select an option

  • Save Himan10/66fa34df1b18f48c1c187c9e566f0cf3 to your computer and use it in GitHub Desktop.

Select an option

Save Himan10/66fa34df1b18f48c1c187c9e566f0cf3 to your computer and use it in GitHub Desktop.
Regex and OS based searching from any location/path.
# Regex -> Searching , Sending
import os
import re
import shutil
import sys
from time import sleep
from pprint import pprint
found_files = []
found_paths = []
# == Searching files (extension based) with os.walk()
def waiting():
sys.stdout.write("\r")
sys.stdout.write(" \t\t --> Processing is on the way <-- ")
sys.stdout.flush()
def shutil_send(source):
try:
shutil.copy(source, os.path.join(os.getcwd(), "found_files"))
except PermissionError:
pass
def searching_files():
ret = 0
ignore_list = [
"/home/hi-man/python/pyproject",
"/home/hi-man/logs",
"/home/hi-man/Templates",
"/home/hi-man/Desktop",
]
regex_pattern = re.compile(f"(.*?)[.]*{sys.argv[1]}$", re.I)
path = input("\n Tell me the path ('/home/user-name/path) : ")
print("\n")
if os.path.exists(path) is not True:
raise Exception(" Sorry Wrong Path ")
for i, j, k in os.walk(path):
if i in ignore_list:
j[:] = []
k[:] = []
for every_k in k:
check = re.search(regex_pattern, every_k)
waiting()
if check is not None:
found_files.append(check.group())
found_paths.append(os.path.join(i, check.group()))
continue
sys.stdout.write("\r \t\t --> Hurray! Searching Completed <--\n")
sleep(1)
if found_files:
ret = 1
return ret
def sending_files(send_choice):
if not (os.path.exists(os.path.abspath("./found_files"))):
print("\n Create a Directory -> found_files")
os.mkdir("found_files")
pass
if send_choice == "yes":
result_file = open(os.path.abspath("./found_files/CHECK_PATHS.txt"), "w")
for every_path in found_paths:
try:
result_file.write(every_path + "\n")
shutil_send(every_path)
waiting()
except shutil.SameFileError:
print(" Actually there was same files ")
break
sys.stdout.write("\r \t\t--> Hurray! Sending Complete <--\n")
else:
print(" --- Sending Cancelled ---")
# == Show the files at terminal
def show_terminal(choice):
if choice == "names":
for every_file in found_files:
pprint(f" {found_files.index(every_file)} -> {every_file}")
elif choice == "paths":
for every_path in found_paths:
pprint(f" {found_paths.index(every_path)} -> {every_path}")
else:
print(" I'm not a stupid like you ")
# Main Army Goes from here
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.stderr.write(" Usage - python <filename> <extension>\n")
print(" extension - py,txt,7z,bz,mp3,pdf,exe ... ")
sys.exit()
else:
try:
ans = searching_files()
except Exception as err:
print(str(err))
sys.exit()
if ans == 1:
print(" Something is found \n")
print(" -> Check NAMES \n -> Check full PATHS ")
choice = input("\n Tell me ? ").lower()
show_terminal(choice)
send_choice = input("\n Send files -> found_files : ").lower()
sending_files(send_choice)
else:
print(" Nothing Found here ")
exit()
print("\n -- Thanks ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment