def get_files(path=getcwd()): """path=getcwd() ---> generateur(path) ---> files in path""" if path == "" : path = getcwd() path = decode(path).replace("\\","/") if path[-1] != "/" : path += "/" files = listdir(path) for i in files: i = decode(i) if is_file(path + i): yield i
def get_dirs(path=getcwd()): """path=getcwd() ---> generateur(dirs)""" if path == "" : path = getcwd() path = decode(path).replace("\\","/") if path[-1] != "/" : path += "/" dirs = listdir(path) path_start = getcwd() for i in dirs: i = decode(i) if is_dir(path + i): yield i + "/"
def get_dirs(path=getcwd()): """path=getcwd() ---> generateur(dirs)""" if path == "": path = getcwd() path = decode(path).replace("\\", "/") if path[-1] != "/": path += "/" dirs = listdir(path) path_start = getcwd() for i in dirs: i = decode(i) if is_dir(path + i): yield i + "/"
def get_paths2(path=getcwd()): """generateur renvoyant toutes les urls filles du dossier parent""" if path == "" : path = getcwd() path = decode(path).replace("\\","/") if path[-1] != "/" : path += "/" yield path for i in get_dirs(path): try : i = decode(i) for p in get_paths2(path+i) : yield p except : yield "erreur"
def get_allpaths(path=getcwd()): """retourne toutes les urls du dossier parent (les urls peuvent etre en plusieurs exemplaires)""" if path == "" : path = getcwd() path = decode(path).replace("\\","/") if path[-1] != "/" : path += "/" list_path = [] list_dossiers = [path] for i in get_dirs(path): i = path + decode(i) list_dossiers.append(i) recup=get_allpaths(i) if recup != []: for x in recup: list_dossiers.append(decode(x)) return list_dossiers
def get_paths(path=getcwd()): """version ameliore de "get_allpaths(), les urls ne sont qu'en un seul exemplaire""" if path == "" : path = getcwd() path = decode(path) p = get_allpaths(path) paths = [] for i in p : if i not in paths : paths.append(i) return paths
def get_name(name, with_ext=True): name = decode(name).replace("\\","/") if name: if name[-1] == "/": name = name[ : -1] name = name.split("/")[-1] if with_ext: return name else: name = name.split('.') if not name[0]: return '.'.join(name) else: return name[0]
def get_name(name, with_ext=True): name = decode(name).replace("\\", "/") if name: if name[-1] == "/": name = name[:-1] name = name.split("/")[-1] if with_ext: return name else: name = name.split('.') if not name[0]: return '.'.join(name) else: return name[0]
def get_path(name): if name: if name != "/": name = decode(name) name = name.replace("\\","/") if name[-1] == "/": name = name[:-1] path = "/".join(name.split("/")[:-1]) if name[0] == "/" and not path: return "/" return path+"/"*bool(path) else: return "/" else: return ""
def supprdir(dossier): """supprime un dossier, tous ses sous-dossiers et tous les fichiers contenus""" dossier = decode(dossier) if dossier[-1] != "/" : dossier += "/" try: dirfile = get_dirfile(dossier) for f in dirfile[1]: remove(dossier+f) for d in dirfile[0]: supprdir(dossier+d) rmdir(dossier) return "" except Exception, err: return err[-1]
def copydir(dossier_original,dossier_copy): """copie un dossier, tous ses sous-dossiers et tous les fichiers contenus""" dossier_original = decode(dossier_original) dossier_copy = decode(dossier_copy) if dossier_original[-1] != "/": dossier_original += "/" if dossier_copy[-1] != "/": dossier_copy += "/" try: dossier_copy = name_exists(dossier_copy+get_name(dossier_original[:-1])) try: mkdir(dossier_copy) except Exception, err: print err dirfile = get_dirfile(dossier_original) for d in dirfile[0]: copydir(dossier_original+d,dossier_copy) for f in dirfile[1]: file(dossier_original+f).copy(dossier_copy+f, true) return "" except Exception, err: return err[-1]
def get_allfiles2(path=getcwd()): """generateur renvoyant toutes les paths filles du dossier parent""" if path == "" : path = getcwd() path = decode(path).replace("\\","/") if path[-1] != "/" : path += "/" dirfile = get_dirfile(path) yield (path,dirfile[1]) for i in dirfile[0]: try: for d,f in get_allfiles2(path+i):yield (d,f) except: yield ("erreur",[])
def get_dirfile(path=getcwd()): """path=getcwd() ---> tuple(dirs,files)""" if path == "" : path = getcwd() path = decode(path) if path[-1] != "/" : path += "/" dos = listdir(path) fichier = [] dossier = [] for i in dos : if is_file(path + i): fichier.append(i) elif is_dir(path + i): dossier.append(i) return [dossier,fichier]
def try_name(name): name = decode(name) path = get_path(name) ext = "."+get_ext(name) if ext == ".": ext = "" name = get_name(name) if ext: name = name[:-len(ext)] if path: liste = listdir(path) else: liste = listdir(getcwd()) erreur = 0 temp = name[:]+ext while temp in liste: erreur += 1 temp = name + " ("+str(erreur)+")"+ext return path+temp
def try_file(name): name = decode(name) path = get_path(name) name = get_name(name) ext = "." + get_ext(name) if ext != ".": name = name[:-len(ext)] else: ext = "" erreur = 0 temp_name = name + ext while file_exists(path + temp_name): erreur += 1 temp_name = name+(" ("+str(erreur)+")")+ext return path+temp_name
def get_allfiles(path=getcwd()): if path == "" : path = getcwd() path = decode(path) for p in get_paths2(path): for f in get_files(p) : yield p+f
def get_ext(name): name = get_name(decode(name)) temp = name.split(".")[-1] if temp != name: return temp else: return ""
def get_name(name): name = decode(name).replace("\\","/") if name: if name[-1] == "/": name = name[ : -1] return name.split("/")[-1]
def name_exists(name): name = decode(name) path = get_path(name) if not path: path = getcwd() if get_name(name) in listdir(path): return True else: return False