Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path, PurePosixPath
- from shutil import copyfile
- import re
- import os
- def __main__():
- # will ask user for input again after invalid response or completion
- while(True):
- # input_parser() returns a list
- consider_list = input_parser_1()
- print_list(consider_list)
- interest_list = input_parser_1(consider_list)
- print_list(interest_list)
- take_action(input_parser_2(), interest_list)
- def print_list(x) -> None:
- #x.sort()
- for elem in x:
- print(elem)
- def input_parser_1(consider_list=[]) -> list:
- "receives input and stores the one-letter action and the phrase individually"
- while True:
- response = input()
- action = response[0]
- # Stores general phrase (could be path, text, etc) in p
- p = response[2:]
- # If the inputted action ('D') is in the list of accepted actions ('DR'), proceed to consider_files()
- if action in 'DR':
- return consider_files(p, action)
- elif action in 'ANET<>':
- return interest_files(p, action, consider_list)
- elif action in 'FDT':
- print('hi')
- return action
- else:
- print('ERROR')
- def input_parser_2() -> str:
- while True:
- response = input()
- action = response
- if action in 'FDT':
- return action
- else:
- print('ERROR')
- # separate function to consider files
- def consider_files(p, action, consider_list=[]) -> list:
- "returns a list of considered files"
- if action == 'D':
- for path in list(Path(p).iterdir()):
- if path.is_file():
- consider_list.append(path)
- if action == 'R':
- for path in list(Path(p).iterdir()):
- if path.is_dir():
- consider_files(path, action, consider_list)
- else:
- consider_list.append(path)
- return consider_list
- def interest_files(p, action, consider_list, interest_list=[]) -> list:
- "returns a list of interesting files"
- if action == 'A':
- interest_list = consider_list
- if action == 'N':
- for path in consider_list:
- if p in str(path):
- interest_list.append(path)
- if action == 'E':
- for path in consider_list:
- extension = PurePosixPath(path).suffix
- if p in extension:
- interest_list.append(path)
- if action == 'T':
- for path in consider_list:
- with open(path, 'r') as file:
- for line in file:
- if p in line:
- interest_list.append(path)
- break
- if action == '<':
- for path in consider_list:
- if os.path.getsize(path) < int(p):
- interest_list.append(path)
- if action == '>':
- for path in consider_list:
- if os.path.getsize(path) > int(p):
- interest_list.append(path)
- return interest_list
- def take_action(action, interest_list) -> None:
- if action == 'F':
- for path in interest_list:
- with open(path, 'r') as file:
- print(file.readline().split(' ')[0])
- if action == 'D':
- print("hi")
- for path in interest_list:
- print(path)
- dst = str(path) + '.dup'
- copy(path, dst)
- if action == 'T':
- for path in interest_list:
- os.utime(path)
- if __name__ == '__main__':
- __main__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement