123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import os, shutil
- def get_path_str(path):
-
-
- if (type(path) != str):
- return ""
-
-
- rtn_str = ""
- while (path != ""):
- if (path[0] in "/\\"):
- rtn_str += '/'
- while (len(path) > 0 and path[0] in "/\\"):
- path = path[1:]
- if (len(path) > 0):
- rtn_str += path[0]
- path = path[1:]
-
-
- if (rtn_str[-1] == '/'):
- rtn_str = rtn_str[:-1]
-
-
- return rtn_str
-
- def f_exists(path):
- path = get_path_str(path)
- return os.path.exists(path)
-
- def is_file(path):
- path = get_path_str(path)
- return os.path.isfile(path)
-
- def is_folder(path):
- path = get_path_str(path)
- return os.path.isdir(path)
- def get_file_name(path):
-
-
- if (type(path) != str):
- return None
- path = get_path_str(path)
-
-
- if (f_exists(path)):
- path = os.path.abspath(path)
-
-
- string = ""
- for i in range(len(path) - 1, -1, -1):
- if (path[i] == '/'):
- for j in range(i + 1, len(path)):
- string += path[j]
- break
-
-
- return string
- def get_base_path(path, is_file):
-
-
- if ((type(path) != str) or (type(is_file) != bool)):
- return None
- path = get_path_str(path)
-
-
- if (f_exists(path)):
- path = get_path_str(os.path.abspath(path))
-
-
- if (is_file):
- while (path != ""):
- if (path[-1] == '/'):
- path = path[:-1]
- break
- path = path[:-1]
- return path
- def get_base_folder_name(path, is_file, folder_as_file):
-
-
- if ((type(path) != str) or (type(is_file) != bool) or (type(folder_as_file) != bool)):
- return None
- path = get_path_str(path)
-
- if ((is_file) or (is_file == False and folder_as_file)):
- return get_file_name(get_base_path(path, True))
- return get_file_name(get_base_path(path, False))
- def cp_file(src, dest):
-
-
- if ((f_exists(src) == False) or (is_file(src) == False)):
- return False
-
-
-
-
- if (f_exists(dest) == False):
-
- base_path = get_base_path(dest, True)
-
- if (f_exists(base_path) == False):
- os.makedirs(base_path)
-
- shutil.copy(src, base_path + "/" + get_file_name(dest))
- elif (f_exists(dest) == True and is_file(dest)):
-
- base_path = get_base_path(dest, True)
-
- os.remove(dest)
-
- shutil.copy(src, base_path + "/" + get_file_name(dest))
- elif (f_exists(dest) == True and is_folder(dest)):
-
- shutil.copy(src, dest + "/" + get_file_name(src))
-
-
- return True
- def list_folder_tree(src):
-
-
- if (type(src) != str or f_exists(src) == False):
- return []
-
-
-
- f_list = [src]
- tmp_list = []
- while (True):
-
- tmp_list = []
-
-
- for f in f_list:
-
- if (f not in tmp_list):
- tmp_list.append(f)
-
- if (is_folder(f)):
- for int_f in os.listdir(f):
-
- if ((f + "/" + int_f) not in tmp_list):
- tmp_list.append(f + "/" + int_f)
-
- if (f_list == tmp_list):
- break
-
- f_list = tmp_list
-
- return f_list
-
- def cp_folder(src, dest, treat_as_file):
-
-
- if ((f_exists(src) == False) or (is_folder(src) == False)
- or (type(dest) != str) or (type(treat_as_file) != bool)):
- return False
-
-
- if (f_exists(dest) == False):
- os.makedirs(dest)
-
-
- if (treat_as_file):
- dest = dest + "/" + get_base_folder_name(src, False)
- if (f_exists(dest) == False):
- os.makedirs(dest)
-
-
-
- f_list = list_folder_tree(src)
-
-
- for f in f_list:
- dest_f_path = dest + "/" + f.replace(src, "")
- if (is_file(f)):
- cp_file(f, dest_f_path)
- elif (is_folder(f) and (is_folder(dest_f_path) == False)):
- os.makedirs(dest_f_path)
-
-
- return True
- def rm_file(path):
-
-
- if (type(path) != str or f_exists(path) == False or is_file(path) == False):
- return False
-
-
- os.remove(path)
- return True
-
- def rm_folder(path):
-
-
- if (type(path) != str or f_exists(path) == False or is_folder(path) == False):
- return False
-
-
- f_list = list_folder_tree(path)
- for i in range(len(f_list) - 1, -1, -1):
- if (is_file(f_list[i])):
- rm_file(f_list[i])
- elif (is_folder(f_list[i])):
- os.rmdir(f_list[i])
-
-
- return True
- def get_file_size(path):
-
-
- if (is_file(path) == False):
- return 0
- return os.path.getsize(get_path_str(path))
|