Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- import os.path
- import shutil
- def get_filename_D(input_path: str) -> list:
- '''Return a list of files
- with a given path'''
- list_file = []
- for run in input_path.iterdir():
- if run.is_file():
- list_file.append(run)
- return list_file
- def get_dir_R(input_path: str) -> list:
- '''return a list of directory path
- with a given path'''
- list_dir = []
- for run in input_path.iterdir():
- if run.is_dir():
- list_dir.append(run)
- return list_dir
- def get_filename_R(input_path: str) -> list:
- '''return a list files in a directory
- and also files inside sub-directories
- and so on'''
- list_file = []
- list_dir = []
- list_file += get_filename_D(input_path)
- list_dir += get_dir_R(input_path)
- for run in list_dir:
- list_file += get_filename_R(run)
- return list_file
- def print_filename(list_of_path: list) -> str:
- '''convert a list of path into string
- and print out the string'''
- for n in list_of_path:
- print(n)
- def second_input_a(list_of_path: list) -> list:
- '''from a given list of files, return a
- list of files that is exactly the same'''
- file_interesting = []
- for n in list_of_path:
- file_interesting.append()
- return file_interesting
- def second_input_n(list_of_path: list, name_input: str) -> list:
- '''from a given list of files, return
- a new list of files that has the same
- filename as the one that user inputs'''
- file_interesting = []
- for n in list_of_path:
- if os.path.split(n)[1] == name_input:
- file_interesting.append(n)
- return file_interesting
- def second_input_e(list_of_path: list, name_input: str):
- '''from a given list of files, return
- a new list of files that has the same
- extention as the one that user inputs'''
- file_interesting = []
- for n in list_of_path:
- if os.path.splitext(n)[1] == name_input or os.path.splitext(n)[1][1:] == name_input:
- file_interesting.append(n)
- return file_interesting
- def second_input_t(list_of_path: list, keyword: str) -> list:
- '''from a given list of files, return
- a new list of text files that contains
- a string same as user input'''
- file_interesting = []
- for n in list_of_path:
- try:
- the_file = open(n)
- while True:
- line = the_file.readline()
- if line.find(keyword) != -1:
- file_interesting.append(n)
- if line == '':
- break
- except ValueError:
- pass
- finally:
- if the_file != None:
- the_file.close()
- return file_interesting
- def second_input_smaller(list_of_path: list,name_input: str) -> list:
- '''from a given list of files, return a sublist
- of files whose size is smaller than user input'''
- file_interesting = []
- for n in list_of_path:
- if os.path.getsize(n) < int(name_input):
- file_interesting.append(n)
- return file_interesting
- def second_input_greater(list_of_path: list,name_input: str) -> list:
- '''from a given list of files, return a sublist
- of files whose size is greater than user input'''
- file_interesting = []
- for n in list_of_path:
- if os.path.getsize(n) > int(name_input):
- file_interesting.append(n)
- return file_interesting
- def third_input_f(list_of_path: list) -> str:
- '''from a list of files, print the first line if
- the file is text, and print 'NOT TEXT' if the file
- is not a text file'''
- for n in list_of_path:
- try:
- the_file = open(n)
- line = the_file.readline().strip()
- if line != '':
- print(line)
- the_file.close()
- else:
- print(line)
- the_file.close()
- except ValueError:
- print('NOT TEXT')
- def third_input_dup(list_of_path: list) -> None:
- '''from a list of files, duplicate every file, and
- store it in the same directory'''
- for n in list_of_path:
- string = str(n)
- shutil.copy2(string, string + '.dup')
- def the_first_input():
- while True:
- first_input = input()
- path1 = Path(first_input[2:])
- if path1.is_dir():
- if first_input.startswith('D '):
- file_list = get_filename_D(path1)
- print_filename(file_list)
- return file_list
- elif first_input.startswith('R '):
- file_list = get_filename_R(path1)
- print_filename(file_list)
- return file_list
- else:
- print('Error')
- else:
- print('ERROR')
- def the_second_input(filez_list):
- list_of_interesting = []
- while True:
- second_input = input()
- name_of_file = second_input[2:]
- code = second_input[:2]
- if second_input.strip() == 'A':
- list_of_interesting = filez_list
- print_filename(list_of_interesting)
- return list_of_interesting
- elif code == 'N ':
- print_filename(second_input_n(filez_list,name_of_file))
- list_of_interesting = second_input_n(filez_list,name_of_file)
- return list_of_interesting
- elif code == 'E ' :
- print_filename(second_input_e(filez_list,name_of_file))
- list_of_interesting = second_input_e(filez_list,name_of_file)
- return list_of_interesting
- elif code == 'T ':
- print_filename(second_input_t(filez_list,name_of_file))
- list_of_interesting = second_input_t(filez_list,name_of_file)
- return list_of_interesting
- elif code == '< ':
- try:
- print_filename(second_input_smaller(filez_list,name_of_file))
- list_of_interesting = second_input_smaller(filez_list,name_of_file)
- return list_of_interesting
- except ValueError:
- print('ERROR')
- elif code == '> ':
- try:
- print_filename(second_input_greater(filez_list,name_of_file))
- list_of_interesting = second_input_greater(filez_list,name_of_file)
- return list_of_interesting
- except ValueError:
- print('ERROR')
- else:
- print('ERROR')
- def the_third_input(file_list):
- while True:
- third_input = input()
- if third_input == 'F':
- third_input_f(file_list)
- return True
- elif third_input == 'D':
- third_input_dup(file_list)
- return True
- else:
- print('ERROR')
- def the_whole_puzzle():
- file_list = the_first_input()
- interesting_file_list = the_second_input(file_list)
- the_third_input(interesting_file_list)
- the_whole_puzzle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement