def image_hash_tree(dirs, exts=None, processfunc=None): tree = BKTree(hamming_distance) map = defaultdict(list) exts = exts or fileextensions.images for dir in dirs: for entry in scandir_rec(dir, dirs=True, follow_symlinks=False, allow_skip=True): if entry.is_dir() and entry.name in IGNORE_DIRNAMES: logging.debug("Skipped: %s", entry.path) entry.follow = False elif entry.is_file(): ext = entrysuffix(entry)[1:].lower() if ext in exts: try: img = Image.open(entry.path, "r") except OSError: logging.warning("Cannot open image: %s", entry.path) else: hash = phash_blockmean(img) tree.add(hash) map[hash].append(entry.path) if processfunc: processfunc((entry.path, hash)) return tree, map
def iter_size_path(dirs): for dir in dirs: for entry in scandir_rec(dir, dirs=True, follow_symlinks=False, allow_skip=True): if entry.is_dir() and entry.name in IGNORE_DIRNAMES: logging.debug("Skipped: %s", entry.path) entry.follow = False elif entry.is_file(): filesize = entry.stat().st_size yield filesize, entry.path
def convert_filenames_to_ascii(path, follow_symlinks=False, rec=False): # type: (PathType, bool, bool) -> None """ convert all files in `path` to a ascii representation using unidecode """ for entry in scandir_rec(path, files=True, dirs=False, rec=rec, follow_symlinks=follow_symlinks): filepath = entry.path base, name = os.path.split(filepath) os.rename(filepath, os.path.join(base, unidecode(name)))
def from_fs(cls, dirpath, hashcls=hashlib.sha1): # type: (PathType, HashCls) -> DirHasher """ Creates a DirHasher instance for a filesystem folder. """ paths = [ entry.path for entry in sorted(scandir_rec(dirpath, files=True, dirs=False), key=lambda x: x.path) ] hashes = (hash_file(path, hashcls).digest() for path in paths) return cls(paths, CachedIterable(hashes), fspath(dirpath))
def do(paths, yes): # type: (Iterable[str], bool) -> None for path in paths: for entry in scandir_rec(path, dirs=False, files=True): stats = entry.stat() try: if not is_writeable(stats): if yes or confirm("Make {} writeable?".format(entry.path)): make_writeable(entry.path, stats) if yes: print("Made {} writeable".format(entry.path)) except Exception as e: logging.exception(entry.path)
nargs=2, type=float, help="Change the rate from x fps to y fps") group.add_argument("--delay", type=float, help="Delay by +x or -x seconds") args = parser.parse_args() if args.inpath.is_file() == args.outpath.is_file(): errorquit("inpath and outpath must be both files or both directories") if args.inpath == args.outpath: errorquit("inpath is not allowed to equal outpath") if args.fps: func = lambda x: x * args.fps[0] / args.fps[1] elif args.delay: func = lambda x: x + args.delay if args.inpath.is_dir(): for infile in scandir_rec(args.inpath, dirs=False, relative=True): outfile = args.outpath / infile.relpath print("In file: " + infile) print("Out file: " + outfile) try: sync_srt(infile.path, outfile, func) except MalformedFileException as e: print("error: file is malformed") else: print("In file: " + args.inpath) print("Out file: " + args.outpath) sync_srt(args.inpath, args.outpath, func)
from __future__ import generator_stop from argparse import ArgumentParser from genutility.file import is_all_byte from genutility.filesystem import scandir_rec parser = ArgumentParser() parser.add_argument("path") args = parser.parse_args() for entry in scandir_rec(args.path, dirs=False, files=True): if entry.stat().st_size != 0: with open(entry.path, "rb") as fr: if is_all_byte(fr, b"\x00"): print(entry.path)