Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. from pathlib import Path
  2.  
  3. #Lists used to hold paths
  4. my_reading = []
  5. my_interesting = []
  6.  
  7. #This function prints out all the files in one directory,
  8. #but not the ones in the subdirectories
  9. def read_dir(file_path, read_list): #funcD
  10.  
  11.     for x in file_path.iterdir():
  12.         if x.is_file():
  13.             print(x)
  14.             read_list.append(str(x.absolute()))
  15.  
  16.  
  17.     for x in file_path.iterdir():
  18.         if x.is_dir():
  19.             print(x)
  20.             #read_list.append(str(x.absolute()))
  21.  
  22.  
  23. #This recursively iterates through directories to
  24. #print out all the folders and files in lexicographical order
  25. def read_sub(file_path, read_list): #funcR
  26.     for x in file_path.iterdir():
  27.         if x.is_file():
  28.             read_list.append(str(x.absolute()))
  29.             print(x)
  30.  
  31.     for x in file_path.iterdir():
  32.         if x.is_dir():
  33.             #read_list.append(str(x.absolute()))
  34.             print(x)
  35.             read_sub(x,read_list)
  36.  
  37. #this checks to see which method to call
  38. #for printing out the contents of the file
  39. def first_part(letter, path):
  40.     while True:
  41.         if letter == 'D':
  42.             read_dir(path, my_reading)
  43.             print(my_reading)
  44.             break
  45.  
  46.         elif letter == "R":
  47.             read_sub(path, my_reading)
  48.             print(my_reading)
  49.             break
  50.  
  51.         else:
  52.             print("Error")
  53.  
  54. #Handles the second part of the asssignment
  55. #involving selecting interesting files
  56. def second_part(letter, path, read_list, interesting_list):
  57.     while True:
  58.         if(letter == 'A'):
  59.             find_files(read_list, interesting_list)
  60.             break
  61.         elif(letter == 'N'):
  62.             find_name(path, read_list, interesting_list)
  63.             break
  64.         elif(letter == 'E'):
  65.             find_suffix(path, read_list, interesting_list)
  66.             break
  67.         elif (letter == 'T'):
  68.             find_text(path, read_list, interesting_list)
  69.             break
  70.         elif (letter == '>' or letter == '<'):
  71.             find_size(letter, path, read_list, interesting_list)
  72.             break
  73.         else:
  74.             print("Error you dummy!")
  75.  
  76.  
  77. #marks all the files as interesting
  78. #and prints them out
  79. def find_files(read_list, interesting_list): #funcA
  80.     for x in read_list:
  81.         if Path(x).is_file():
  82.             interesting_list.append(x)
  83.             print(x)
  84.  
  85. #find files whose name matches the given name
  86. def find_name(name, read_list, interesting_list):
  87.     for file_path in read_list:
  88.         path = Path(file_path)
  89.         path_name = path.name
  90.  
  91.         if path_name == name:
  92.             print("Worked")
  93.             interesting_list.append(path_name)
  94.         else:
  95.             print()
  96.  
  97. #finds files whose extensions matches the given extension
  98. def find_suffix(suffix, read_list, interesting_list):
  99.     if suffix[0] == '.':
  100.         pass
  101.     else:
  102.         suffix = '.' + suffix
  103.  
  104.     for x in read_list:
  105.         if Path(x).suffix == suffix:
  106.             interesting_list.append(x)
  107.         else:
  108.             print('fail')
  109.  
  110. #finds files that contain the given text
  111. def find_text(text, read_list, interesting_list):
  112.     for the_file in read_list:
  113.         name = Path(the_file).name
  114.         if name[0] != '.':
  115.             open(the_file, 'r')
  116.             for line in the_file:
  117.                 if line.find(text, 0, len(line)) != -1:
  118.                     interesting_list.append(the_file)
  119.                     print('yay')
  120.  
  121. #Finds files that are either greater or lesser than the size given
  122. #depending upon the operator passed
  123. def find_size(operator, size, read_list, interesting_list):
  124.     size_int = int(size)
  125.     if(operator == '<'):
  126.         for x in read_list:
  127.             path_object = Path(x)
  128.             file_size = path_object.stat().st_size
  129.             if(file_size < size_int):
  130.                 print(x)
  131.     elif(operator == '>'):
  132.         for x in read_list:
  133.             path_object = Path(x)
  134.             file_size = path_object.stat().st_size
  135.             if(file_size > size_int):
  136.                 print(x)
  137.  
  138. #This is the main function where we call all the above functions
  139. def main():
  140.     i = input()
  141.     path = Path(i[2:])
  142.     first_letter = i[0]
  143.  
  144.     first_part(first_letter, path)
  145.  
  146.     i = input()
  147.     path = i[2:]
  148.     first_letter = i[0]
  149.  
  150.     second_part(first_letter, path, my_reading, my_interesting)
  151.  
  152.  
  153.  
  154.  
  155. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement