def count_files ( path, reset = True, recursive = True ): global totalfiles,totalfolders #path = smart_unicode(path) #print "In count_files(%s)"%path.encode('utf-8') if reset: totalfiles=totalfolders=0 if smart_unicode(path) in Exclude_folders: #si le path est un chemin exclu, on sort return if recursive: try: walk(path, processDirectory, None ) except: walk(path.encode('utf-8'), processDirectory, None) else: try: path,folders,files = oswalk(path).next() except: # new in 0.7.1.3 path,folders,files = oswalk(path.encode('utf-8')).next() totalfiles=totalfiles+len(files) totalfolders=totalfolders+int(len(files)>=1)
def get_filenames(root_dir): """ Walks 'root_dir' in order to obtain a list of *.mkv/*.mp4 files. :param root_dir: Root directory to walk :param vid_type: Either 'movies' or 'tv' :return: Either a list of files or an error message """ videos = [] for main_dir, sub_dirs, files in oswalk(root_dir): for file in files: if ossplitext(file)[1] in (".mkv", ".mp4"): videos.append(ospathjoin(main_dir, file)) if len(videos) > 0: return videos else: log_msg("error", "No video files found in {}".format(root_dir)) return False
def match_file_generator(): for (root, dirs, files) in oswalk(top='data.files', topdown=True): for file in files: if not str(file).endswith('dict') and not str(file).endswith( 'statuses'): yield f"{root}\{file}"
def iter_dir(dpath, pattern='*'): steps = (step[::2] for step in oswalk(dpath)) return {f'{path}{sep}{f}':f for path, files in steps for f in fnfilter(files, pattern)}
def iterdir_fnfilter(pattern): return fnfilter([f for p, d, fs in oswalk('/home/erik/Downloads') for f in fs], pattern)
def iterdir_fnmatch(pattern): yield from (f for p, d, fs in oswalk('/home/erik/Downloads') for f in fs if fnmatch(f, pattern))
def iterdir_regexp(pattern): yield from filter(pattern.search, (f for p, d, fs in oswalk('/home/erik/Downloads') for f in fs))
def pathjoin3(): for path, files in path_data: yield from (f'{path}{s}{f}' for f in files) @time_this(2500) def pathjoin4(): for path, files in path_data: path += s yield from (f'{path}{f}' for f in files) @time_this(2500) def pathjoin5(): for path, files in path_data: yield from (osjoin(path, f) for f in files) osjoin = pathjoin path_data = [(p, fnfilter(fs, '*.py')) for p, d, fs in oswalk('/home/erik/Downloads')] s = sep sjoin = s.join pathjoin0() pathjoin1() pathjoin2() pathjoin3() pathjoin4() pathjoin5()
def empty_directory(self, delpath): for root, dirs, files in oswalk(delpath): for f in files: unlink(path.join(root, f)) for d in dirs: shutil.rmtree(path.join(root, d))
def sanewalk(dir): """ A sane version of os.walk, yielding just paths. """ for (root, dirs, files) in oswalk(dir): for file in files: yield path.join(root, file)
pkipath = pkipath[:len(pkipath) - 1] if ospath.realpath(pkipath) != pkipath: print('ERROR: pki is a symlink, refusing to init pki') if not ospath.isdir(pkipath): res = { 'error': True, 'message': "ERROR: Pki init failed, your pki path is already used" } print(res) exit(1) else: dirnames = [] filenames = [] for path, dirs, files in oswalk(pkipath): for d in dirs: dirnames.append(d) for f in files: filenames.append(f) if 'public_key.pem' not in filenames: initiated = False print("First init has to be done") dirlen = len(dirnames) if dirlen < 6 and dirlen > 0 and initiated: print("Unproper PKI filesystem missing directories") elif dirlen != 6 and dirlen > 0 and not initiated: print( "Your PKI filesystem seems to contains partial old structure. You should backup and init again"
def match_file_generator(): for (root, dirs, files) in oswalk(top='data.files/match.files', topdown=True): for file in files: yield f"{root}\{file}"
def series_id_generator(): for (root, dirs, files) in oswalk(f'data.files', topdown=True): for dir in dirs: yield dir