def ExistsPath(self, path, old=False, stop=False, mkdir=False, touch=False, delete=False): ''' Parameters ---------- path: str | list/tuple of str old: =True: rename the exists file with '_old' =False | None: don't rename stop: =True: if not exists, raise error mkdir: True | False If not exists, mkdir it touch: True | False If not exists, touch it delete: True | False If exists, delete it Returns ---------- If path is str: return True/False If path is list/tuple of str: return list of True/False ''' import os from jizhipy.Basic import Raise, IsType if (IsType.isstr(path)): islist, path = False, [path] else: islist, path = True, list(path) info, delname, trfa, pathabs = '', '', [], [] for i in range(len(path)): pathabs.append(self.AbsPath(path[i])) trfa.append(os.path.exists(pathabs[-1])) if (trfa[-1] and IsType.isdir(pathabs[-1])): pathabs[-1] = self.CheckDir(pathabs[-1]) #---------------------------------------- # stop if (stop): for i in range(len(trfa)): if (trfa[i]): continue info += 'Not exists: ' + path[i] + '\n' if (len(info) > 0): Raise(Exception, info[:-1]) # old if (old): for i in range(len(trfa)): if (not trfa[i]): continue if (pathabs[i][-1] == '/'): pathold = pathabs[i][:-1] + '_old/' else: n = pathabs[i].rfind('.') if (n < 0): pathold = pathabs[i] + '_old' else: pathold = pathabs[i][:n] + '_old' + pathabs[i][n:] os.system('mv ' + pathabs[i] + ' ' + pathold) # mkdir or touch if (mkdir): for i in range(len(trfa)): if (trfa[i] is False): os.makedirs(path[i]) if (touch): for i in range(len(trfa)): if (trfa[i] is True): continue if (path[i][-1] == '/'): print('Warning: ' + path[i] + ' is a directory, can NOT touch') continue dirname = os.path.dirname(path[i]) if (not os.path.exists(dirname)): os.makedirs(dirname) os.system('touch ' + path[i]) # delete if (delete): for i in range(len(trfa)): if (not trfa[i]): continue delname += pathabs[i] + ' ' if (len(delname) > 0): os.system('/bin/rm -r ' + delname) if (not islist): trfa = trfa[0] return trfa