Advertisement
Guest User

Project1 python

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. from pathlib import Path
  2. import os.path
  3. import shutil
  4.  
  5. def get_filename_D(input_path: str) -> list:
  6. '''Return a list of files
  7. with a given path'''
  8. list_file = []
  9. for run in input_path.iterdir():
  10. if run.is_file():
  11. list_file.append(run)
  12. return list_file
  13. def get_dir_R(input_path: str) -> list:
  14. '''return a list of directory path
  15. with a given path'''
  16. list_dir = []
  17. for run in input_path.iterdir():
  18. if run.is_dir():
  19. list_dir.append(run)
  20. return list_dir
  21. def get_filename_R(input_path: str) -> list:
  22. '''return a list files in a directory
  23. and also files inside sub-directories
  24. and so on'''
  25. list_file = []
  26. list_dir = []
  27. list_file += get_filename_D(input_path)
  28. list_dir += get_dir_R(input_path)
  29. for run in list_dir:
  30. list_file += get_filename_R(run)
  31. return list_file
  32.  
  33. def print_filename(list_of_path: list) -> str:
  34. '''convert a list of path into string
  35. and print out the string'''
  36. for n in list_of_path:
  37. print(n)
  38. def second_input_a(list_of_path: list) -> list:
  39. '''from a given list of files, return a
  40. list of files that is exactly the same'''
  41. file_interesting = []
  42. for n in list_of_path:
  43. file_interesting.append()
  44. return file_interesting
  45.  
  46. def second_input_n(list_of_path: list, name_input: str) -> list:
  47. '''from a given list of files, return
  48. a new list of files that has the same
  49. filename as the one that user inputs'''
  50. file_interesting = []
  51. for n in list_of_path:
  52. if os.path.split(n)[1] == name_input:
  53. file_interesting.append(n)
  54. return file_interesting
  55.  
  56. def second_input_e(list_of_path: list, name_input: str):
  57. '''from a given list of files, return
  58. a new list of files that has the same
  59. extention as the one that user inputs'''
  60. file_interesting = []
  61. for n in list_of_path:
  62. if os.path.splitext(n)[1] == name_input or os.path.splitext(n)[1][1:] == name_input:
  63. file_interesting.append(n)
  64. return file_interesting
  65. def second_input_t(list_of_path: list, keyword: str) -> list:
  66. '''from a given list of files, return
  67. a new list of text files that contains
  68. a string same as user input'''
  69. file_interesting = []
  70. for n in list_of_path:
  71. try:
  72. the_file = open(n)
  73. while True:
  74. line = the_file.readline()
  75. if line.find(keyword) != -1:
  76. file_interesting.append(n)
  77. if line == '':
  78. break
  79. except ValueError:
  80. pass
  81. finally:
  82. if the_file != None:
  83. the_file.close()
  84. return file_interesting
  85. def second_input_smaller(list_of_path: list,name_input: str) -> list:
  86. '''from a given list of files, return a sublist
  87. of files whose size is smaller than user input'''
  88. file_interesting = []
  89. for n in list_of_path:
  90. if os.path.getsize(n) < int(name_input):
  91. file_interesting.append(n)
  92. return file_interesting
  93.  
  94. def second_input_greater(list_of_path: list,name_input: str) -> list:
  95. '''from a given list of files, return a sublist
  96. of files whose size is greater than user input'''
  97. file_interesting = []
  98. for n in list_of_path:
  99. if os.path.getsize(n) > int(name_input):
  100. file_interesting.append(n)
  101. return file_interesting
  102.  
  103. def third_input_f(list_of_path: list) -> str:
  104. '''from a list of files, print the first line if
  105. the file is text, and print 'NOT TEXT' if the file
  106. is not a text file'''
  107. for n in list_of_path:
  108. try:
  109. the_file = open(n)
  110. line = the_file.readline().strip()
  111. if line != '':
  112. print(line)
  113. the_file.close()
  114. else:
  115. print(line)
  116. the_file.close()
  117. except ValueError:
  118. print('NOT TEXT')
  119. def third_input_dup(list_of_path: list) -> None:
  120. '''from a list of files, duplicate every file, and
  121. store it in the same directory'''
  122. for n in list_of_path:
  123. string = str(n)
  124. shutil.copy2(string, string + '.dup')
  125. def the_first_input():
  126. while True:
  127. first_input = input()
  128. path1 = Path(first_input[2:])
  129. if path1.is_dir():
  130. if first_input.startswith('D '):
  131. file_list = get_filename_D(path1)
  132. print_filename(file_list)
  133. return file_list
  134. elif first_input.startswith('R '):
  135. file_list = get_filename_R(path1)
  136. print_filename(file_list)
  137. return file_list
  138. else:
  139. print('Error')
  140. else:
  141. print('ERROR')
  142. def the_second_input(filez_list):
  143. list_of_interesting = []
  144. while True:
  145. second_input = input()
  146. name_of_file = second_input[2:]
  147. code = second_input[:2]
  148. if second_input.strip() == 'A':
  149. list_of_interesting = filez_list
  150. print_filename(list_of_interesting)
  151. return list_of_interesting
  152. elif code == 'N ':
  153. print_filename(second_input_n(filez_list,name_of_file))
  154. list_of_interesting = second_input_n(filez_list,name_of_file)
  155. return list_of_interesting
  156. elif code == 'E ' :
  157. print_filename(second_input_e(filez_list,name_of_file))
  158. list_of_interesting = second_input_e(filez_list,name_of_file)
  159. return list_of_interesting
  160. elif code == 'T ':
  161. print_filename(second_input_t(filez_list,name_of_file))
  162. list_of_interesting = second_input_t(filez_list,name_of_file)
  163. return list_of_interesting
  164. elif code == '< ':
  165. try:
  166. print_filename(second_input_smaller(filez_list,name_of_file))
  167. list_of_interesting = second_input_smaller(filez_list,name_of_file)
  168. return list_of_interesting
  169. except ValueError:
  170. print('ERROR')
  171.  
  172. elif code == '> ':
  173. try:
  174. print_filename(second_input_greater(filez_list,name_of_file))
  175. list_of_interesting = second_input_greater(filez_list,name_of_file)
  176. return list_of_interesting
  177. except ValueError:
  178. print('ERROR')
  179. else:
  180. print('ERROR')
  181. def the_third_input(file_list):
  182. while True:
  183. third_input = input()
  184. if third_input == 'F':
  185. third_input_f(file_list)
  186. return True
  187. elif third_input == 'D':
  188. third_input_dup(file_list)
  189. return True
  190. else:
  191. print('ERROR')
  192.  
  193.  
  194. def the_whole_puzzle():
  195. file_list = the_first_input()
  196. interesting_file_list = the_second_input(file_list)
  197. the_third_input(interesting_file_list)
  198.  
  199. the_whole_puzzle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement