Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- #Lists used to hold paths
- my_reading = []
- my_interesting = []
- #This function prints out all the files in one directory,
- #but not the ones in the subdirectories
- def read_dir(file_path, read_list): #funcD
- for x in file_path.iterdir():
- if x.is_file():
- print(x)
- read_list.append(str(x.absolute()))
- for x in file_path.iterdir():
- if x.is_dir():
- print(x)
- #read_list.append(str(x.absolute()))
- #This recursively iterates through directories to
- #print out all the folders and files in lexicographical order
- def read_sub(file_path, read_list): #funcR
- for x in file_path.iterdir():
- if x.is_file():
- read_list.append(str(x.absolute()))
- print(x)
- for x in file_path.iterdir():
- if x.is_dir():
- #read_list.append(str(x.absolute()))
- print(x)
- read_sub(x,read_list)
- #this checks to see which method to call
- #for printing out the contents of the file
- def first_part(letter, path):
- while True:
- if letter == 'D':
- read_dir(path, my_reading)
- print(my_reading)
- break
- elif letter == "R":
- read_sub(path, my_reading)
- print(my_reading)
- break
- else:
- print("Error")
- #Handles the second part of the asssignment
- #involving selecting interesting files
- def second_part(letter, path, read_list, interesting_list):
- while True:
- if(letter == 'A'):
- find_files(read_list, interesting_list)
- break
- elif(letter == 'N'):
- find_name(path, read_list, interesting_list)
- break
- elif(letter == 'E'):
- find_suffix(path, read_list, interesting_list)
- break
- elif (letter == 'T'):
- find_text(path, read_list, interesting_list)
- break
- elif (letter == '>' or letter == '<'):
- find_size(letter, path, read_list, interesting_list)
- break
- else:
- print("Error you dummy!")
- #marks all the files as interesting
- #and prints them out
- def find_files(read_list, interesting_list): #funcA
- for x in read_list:
- if Path(x).is_file():
- interesting_list.append(x)
- print(x)
- #find files whose name matches the given name
- def find_name(name, read_list, interesting_list):
- for file_path in read_list:
- path = Path(file_path)
- path_name = path.name
- if path_name == name:
- print("Worked")
- interesting_list.append(path_name)
- else:
- print()
- #finds files whose extensions matches the given extension
- def find_suffix(suffix, read_list, interesting_list):
- if suffix[0] == '.':
- pass
- else:
- suffix = '.' + suffix
- for x in read_list:
- if Path(x).suffix == suffix:
- interesting_list.append(x)
- else:
- print('fail')
- #finds files that contain the given text
- def find_text(text, read_list, interesting_list):
- for the_file in read_list:
- name = Path(the_file).name
- if name[0] != '.':
- open(the_file, 'r')
- for line in the_file:
- if line.find(text, 0, len(line)) != -1:
- interesting_list.append(the_file)
- print('yay')
- #Finds files that are either greater or lesser than the size given
- #depending upon the operator passed
- def find_size(operator, size, read_list, interesting_list):
- size_int = int(size)
- if(operator == '<'):
- for x in read_list:
- path_object = Path(x)
- file_size = path_object.stat().st_size
- if(file_size < size_int):
- print(x)
- elif(operator == '>'):
- for x in read_list:
- path_object = Path(x)
- file_size = path_object.stat().st_size
- if(file_size > size_int):
- print(x)
- #This is the main function where we call all the above functions
- def main():
- i = input()
- path = Path(i[2:])
- first_letter = i[0]
- first_part(first_letter, path)
- i = input()
- path = i[2:]
- first_letter = i[0]
- second_part(first_letter, path, my_reading, my_interesting)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement