file_ops.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # my functions for file creation/deletion operations
  2. # also for file path string stuff
  3. import os, shutil
  4. # function to format a path string correctly:
  5. # - no duplicate slashes
  6. # - no backward slashes
  7. # - nothing ends with a slash
  8. def get_path_str(path):
  9. # check params
  10. if (type(path) != str):
  11. return ""
  12. # else work on the string
  13. rtn_str = ""
  14. while (path != ""):
  15. if (path[0] in "/\\"):
  16. rtn_str += '/'
  17. while (len(path) > 0 and path[0] in "/\\"):
  18. path = path[1:]
  19. if (len(path) > 0):
  20. rtn_str += path[0]
  21. path = path[1:]
  22. # check if there is an ending slash
  23. if (rtn_str[-1] == '/'):
  24. rtn_str = rtn_str[:-1]
  25. # done!
  26. return rtn_str
  27. # function to check if a file/folder path exists or not
  28. def f_exists(path):
  29. path = get_path_str(path)
  30. return os.path.exists(path)
  31. # function to check if a file path actually points to a file
  32. def is_file(path):
  33. path = get_path_str(path)
  34. return os.path.isfile(path)
  35. # function to check if a folder path actually points to a folder
  36. def is_folder(path):
  37. path = get_path_str(path)
  38. return os.path.isdir(path)
  39. # function to get the file name portion of a file path
  40. def get_file_name(path):
  41. # check params
  42. if (type(path) != str):
  43. return None
  44. path = get_path_str(path)
  45. # get the full path of the file/folder
  46. if (f_exists(path)):
  47. path = os.path.abspath(path)
  48. # start reading path string
  49. string = ""
  50. for i in range(len(path) - 1, -1, -1):
  51. if (path[i] == '/'):
  52. for j in range(i + 1, len(path)):
  53. string += path[j]
  54. break
  55. # done!
  56. return string
  57. # function to get the base path of a file or folder
  58. def get_base_path(path, is_file):
  59. # check params
  60. if ((type(path) != str) or (type(is_file) != bool)):
  61. return None
  62. path = get_path_str(path)
  63. # get the full path of the file/folder
  64. if (f_exists(path)):
  65. path = get_path_str(os.path.abspath(path))
  66. # work on path
  67. if (is_file):
  68. while (path != ""):
  69. if (path[-1] == '/'):
  70. path = path[:-1]
  71. break
  72. path = path[:-1]
  73. return path
  74. # function to get the base path of a file or folder
  75. def get_base_folder_name(path, is_file, folder_as_file):
  76. # check params
  77. if ((type(path) != str) or (type(is_file) != bool) or (type(folder_as_file) != bool)):
  78. return None
  79. path = get_path_str(path)
  80. # reuse get_base_path() and get_file_name()
  81. if ((is_file) or (is_file == False and folder_as_file)):
  82. return get_file_name(get_base_path(path, True))
  83. return get_file_name(get_base_path(path, False))
  84. # function to copy a file to another location
  85. def cp_file(src, dest):
  86. # check params
  87. if ((f_exists(src) == False) or (is_file(src) == False)):
  88. return False
  89. # depending on dest 2 things can happen
  90. # - dest defines a new name for the file to create/replace
  91. # - dest is just a folder
  92. if (f_exists(dest) == False): # copy as a new file in that location
  93. # get the base path of dest
  94. base_path = get_base_path(dest, True)
  95. # create the base path if it does not exist
  96. if (f_exists(base_path) == False):
  97. os.makedirs(base_path)
  98. # copy the file to the directory
  99. shutil.copy(src, base_path + "/" + get_file_name(dest))
  100. elif (f_exists(dest) == True and is_file(dest)):
  101. # get the base path of dest
  102. base_path = get_base_path(dest, True)
  103. # remove the file at dest
  104. os.remove(dest)
  105. # copy the file in src to dest
  106. shutil.copy(src, base_path + "/" + get_file_name(dest))
  107. elif (f_exists(dest) == True and is_folder(dest)):
  108. # copy the file in src to dest
  109. shutil.copy(src, dest + "/" + get_file_name(src))
  110. # done!
  111. return True
  112. # function to return a folder tree (file/folders)
  113. def list_folder_tree(src):
  114. # check params
  115. if (type(src) != str or f_exists(src) == False):
  116. return []
  117. # get the list of all files inside the mod folder first (files/folders)
  118. # infinite loop moment
  119. f_list = [src]
  120. tmp_list = []
  121. while (True):
  122. # reset tmp_list
  123. tmp_list = []
  124. # add the new files in f_list folders
  125. # that are not present in it to tmp_list
  126. for f in f_list:
  127. # add new folder/file
  128. if (f not in tmp_list):
  129. tmp_list.append(f)
  130. # if f is a folder check its contents so they can be added to tmp_list
  131. if (is_folder(f)):
  132. for int_f in os.listdir(f):
  133. # add new folder/file
  134. if ((f + "/" + int_f) not in tmp_list):
  135. tmp_list.append(f + "/" + int_f)
  136. # when there is nothing more new added to tmp_list end the infinite loop
  137. if (f_list == tmp_list):
  138. break
  139. # otherwise keep adding the mising files
  140. f_list = tmp_list
  141. # ^ I honestly wasn't expecting this loop to work at first lolll
  142. return f_list
  143. # function to copy a folder to another folder (a file like copy)
  144. def cp_folder(src, dest, treat_as_file):
  145. # check params
  146. if ((f_exists(src) == False) or (is_folder(src) == False)
  147. or (type(dest) != str) or (type(treat_as_file) != bool)):
  148. return False
  149. # check the existence of dest
  150. if (f_exists(dest) == False):
  151. os.makedirs(dest)
  152. # check if src is to be treated as a file (i.e. copy the folder as well and not just its contents)
  153. if (treat_as_file):
  154. dest = dest + "/" + get_base_folder_name(src, False)
  155. if (f_exists(dest) == False):
  156. os.makedirs(dest)
  157. # get the list of all files inside the mod folder first (files/folders)
  158. # infinite loop moment
  159. f_list = list_folder_tree(src)
  160. # start copying the files
  161. for f in f_list:
  162. dest_f_path = dest + "/" + f.replace(src, "")
  163. if (is_file(f)):
  164. cp_file(f, dest_f_path)
  165. elif (is_folder(f) and (is_folder(dest_f_path) == False)):
  166. os.makedirs(dest_f_path)
  167. # done!
  168. return True
  169. # function to delete a file
  170. def rm_file(path):
  171. # check params
  172. if (type(path) != str or f_exists(path) == False or is_file(path) == False):
  173. return False
  174. # remove the file
  175. os.remove(path)
  176. return True
  177. # function to delete a folder with/without contents, don't care if the folder does not exist
  178. def rm_folder(path):
  179. # check params
  180. if (type(path) != str or f_exists(path) == False or is_folder(path) == False):
  181. return False
  182. # remove the folder (get the tree list)
  183. f_list = list_folder_tree(path)
  184. for i in range(len(f_list) - 1, -1, -1):
  185. if (is_file(f_list[i])):
  186. rm_file(f_list[i])
  187. elif (is_folder(f_list[i])):
  188. os.rmdir(f_list[i])
  189. # done!
  190. return True
  191. # function to get the size of a file
  192. def get_file_size(path):
  193. # check params
  194. if (is_file(path) == False):
  195. return 0
  196. return os.path.getsize(get_path_str(path))