Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created October 3, 2020 08:57
Show Gist options
  • Save shinysu/ec3548de2ec0b41d398e152ad9c7e55a to your computer and use it in GitHub Desktop.
Save shinysu/ec3548de2ec0b41d398e152ad9c7e55a to your computer and use it in GitHub Desktop.
import os
import datetime
file_data ={}
todaysDate = datetime.datetime.today()
def get_all_files(filepath):
for (root, directories, files) in os.walk(filepath):
for name in files:
file = os.path.join(root, name)
extension = file.split('.')[-1]
add_to_filedata(file, extension)
def get_latest_files():
modified_files = []
for (root, directories, files) in os.walk(filepath):
for name in files:
file = os.path.join(root, name)
if is_latest(file):
modified_files.append(file)
return modified_files
def is_latest(file):
modifyDate = datetime.datetime.fromtimestamp(os.path.getmtime(file))
modifyDateLimit = modifyDate + datetime.timedelta(days=1)
return modifyDateLimit > todaysDate
def add_to_filedata(name, extension):
if extension in file_data:
file_data[extension].append(name)
else:
file_data[extension] = [name]
def print_file_by_extension(extension):
for file in file_data[extension]:
print(file)
def print_all_files():
for type in file_data:
print(type)
for file in file_data[type]:
print("\t",file)
def print_file_types():
for type in file_data:
print(type)
def print_modified_files(modified_files):
for file in modified_files:
print(file)
print("Hello! I am a file-finder bot! I can find you files in your machine")
print("What would you like to search?")
choice = 'yes'
while choice == 'yes':
option = int(input("Type your option\n1 --> Display all files in directory\n2 --> Search by filetype\n"
"3 --> Get files modified yesterday\n"))
filepath = input("Enter the file path: ").strip('"\'')
get_all_files(filepath)
if option == 1:
print_all_files()
elif option == 2:
print_file_types()
type = input("Enter the filetype: \n").lower()
print_file_by_extension(type)
elif option == 3:
modified_files = get_latest_files()
print_modified_files(modified_files)
choice = input("Would you like to do another search?\n").lower()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment