How to use scandir - 10 common examples

To help you get started, we’ve selected a few scandir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github benhoyt / scandir / test / test_scandir.py View on Github external
def setUp(self):
            self.scandir_func = scandir.scandir_generic
            self.has_file_attributes = False
            TestMixin.setUp(self)
github benhoyt / scandir / test / test_scandir.py View on Github external
def setUp(self):
                self.scandir_func = scandir.scandir_python
                self.has_file_attributes = True
                TestMixin.setUp(self)
github benhoyt / scandir / test / test_scandir.py View on Github external
def setUp(self):
                self.scandir_func = scandir.scandir_c
                self.has_file_attributes = True
                TestMixin.setUp(self)
github benhoyt / scandir / test / test_scandir.py View on Github external
def test_iter_returns_dir_entry(self):
            it = scandir.scandir(TEST_PATH)
            entry = next(it)
            assert isinstance(entry, scandir.DirEntry)
github bschollnick / QuickBBS / quickbbs / directory_caching / __init__.py View on Github external
scan_directory string before sending it to cache.
        """
        directories = {}
        files = {}
        if os.path.exists(scan_directory) is not True:
            return None
        norm_dir_name = scan_directory.strip()
        self.d_cache[norm_dir_name] = {}
        self.d_cache[norm_dir_name]["last_sort"] = None
        dirname = os.path.split(scan_directory)[0]
        if self.filter_filenames is not None:
            utilities.rename_path_to_clean(dirname)
            dirname = utilities.clean_path(dirname)
            utilities.check_files_in_directory(dirname)

        for s_entry in scandir.scandir(scan_directory):
            data = DirEntry()
            data.st = s_entry.stat()
            if s_entry.name.strip().lower() in self.files_to_ignore:
                continue
            if self.hidden_dot_files and s_entry.name.startswith("."):
                continue

            data.fq_filename = os.path.join(
                os.path.realpath(scan_directory.strip()),
                s_entry.name)
            data.parentdirectory = os.path.split(scan_directory)[0:-1][0]
            data.human_st_mtime = time.asctime(
                time.localtime(data.st[stat.ST_MTIME]))

            data.is_dir = s_entry.is_dir()
#            print (s_entry.is_dir())
github twiddli / happypanda / version / utils.py View on Github external
# if gallery has chapters divided into sub folders
        if len(chapters) != 0:
            log_d('Chapters divided in folders..')
            for ch in chapters:
                chap = chap_container.create_chapter()
                chap.title = title_parser(ch)['title']
                chap.path = os.path.join(path, ch)
                metafile.update(GMetafile(chap.path))
                chap.pages = len([x for x in scandir.scandir(chap.path) if x.name.endswith(IMG_FILES)])

        else: #else assume that all images are in gallery folder
            chap = chap_container.create_chapter()
            chap.title = title_parser(os.path.split(path)[1])['title']
            chap.path = path
            metafile.update(GMetafile(path))
            chap.pages = len([x for x in scandir.scandir(path) if x.name.endswith(IMG_FILES)])

    except NotADirectoryError:
        if path.endswith(ARCHIVE_FILES):
            gallery_object.is_archive = 1
            log_i("Gallery source is an archive")
            archive_g = sorted(check_archive(path))
            for g in archive_g:
                chap = chap_container.create_chapter()
                chap.path = g
                chap.in_archive = 1
                metafile.update(GMetafile(g, path))
                arch = ArchiveFile(path)
                chap.pages = len(arch.dir_contents(g))
                arch.close()

    metafile.apply_gallery(gallery_object)
github wadhwasahil / Video-Classification-2-Stream-CNN / temporal / temporal_vid2img.py View on Github external
def data_prep():
    print 'Starting with data prep'
    with open('../dataset/frame_count.pickle','rb') as f1:
        frame_count=pickle.load(f1)
    with open('../dataset/merged_data.pickle','rb') as f2:
        merged_data=pickle.load(f2)
    print 'Loaded dictionary'
    root = './of_images'
    path = os.path.join(root, '')
    data={}
    misplaced_data=[]
    count=0
    for path, subdirs, files in scandir.walk(root):
        for filename in files:
            print filename + '  ' + str(count)
            count+=1
            try:
                vidname=filename.split('_',1)[1].split('.')[0]
                fc=frame_count[vidname]


                for i,j in enumerate(merged_data[vidname]):
                    if j:
                        index=i
                        break
                for i in range(1,(fc/50)+1):
                    data[vidname+'@'+str(i)]=index+1
            except:
                misplaced_data.append(filename)
github morpheus65535 / bazarr / libs / subliminal_patch / core.py View on Github external
def _search_external_subtitles(path, languages=None, only_one=False, scandir_generic=False, match_strictness="strict"):
    dirpath, filename = os.path.split(path)
    dirpath = dirpath or '.'
    fn_no_ext, fileext = os.path.splitext(filename)
    fn_no_ext_lower = fn_no_ext.lower()
    subtitles = {}
    _scandir = _scandir_generic if scandir_generic else scandir

    for entry in _scandir(dirpath):
        if (not entry.name or entry.name in ('\x0c', '$', ',', '\x7f')) and not scandir_generic:
            logger.debug('Could not determine the name of the file, retrying with scandir_generic')
            return _search_external_subtitles(path, languages, only_one, True)
        if not entry.is_file(follow_symlinks=False):
            continue

        p = entry.name

        # keep only valid subtitle filenames
        if not p.lower().endswith(SUBTITLE_EXTENSIONS):
            continue

        # not p.lower().startswith(fileroot.lower()) or not
github benhoyt / scandir / benchmark.py View on Github external
scandir.scandir = scandir.scandir_python
    elif options.scandir == 'os':
        if not hasattr(os, 'scandir'):
            print("ERROR: Python 3.5's os.scandir() not found!")
            sys.exit(1)
        scandir.scandir = os.scandir
    elif hasattr(os, 'scandir'):
        scandir.scandir = os.scandir

    if scandir.scandir == getattr(os, 'scandir', None):
        print("Using Python 3.5's builtin os.scandir()")
    elif scandir.scandir == scandir.scandir_c:
        print('Using fast C version of scandir')
    elif scandir.scandir == scandir.scandir_python:
        print('Using slower ctypes version of scandir')
    elif scandir.scandir == scandir.scandir_generic:
        print('Using very slow generic version of scandir')
    else:
        print('ERROR: Unsure which version of scandir we are using!')
        sys.exit(1)

    if hasattr(os, 'scandir'):
        os.walk = os_walk_pre_35
        print('Comparing against pre-Python 3.5 version of os.walk()')
    else:
        print('Comparing against builtin version of os.walk()')

    benchmark(tree_dir, get_size=options.size)
github benhoyt / scandir / benchmark.py View on Github external
help='get size of directory tree while walking')
    parser.add_option('-c', '--scandir', type='choice', choices=['best', 'generic', 'c', 'python', 'os'], default='best',
                      help='version of scandir() to use, default "%default"')
    options, args = parser.parse_args()

    if args:
        tree_dir = args[0]
    else:
        tree_dir = os.path.join(os.path.dirname(__file__), 'benchtree')
        if not os.path.exists(tree_dir):
            print('Creating tree at {0}: depth={1}, num_dirs={2}, num_files={3}'.format(
                tree_dir, DEPTH, NUM_DIRS, NUM_FILES))
            create_tree(tree_dir)

    if options.scandir == 'generic':
        scandir.scandir = scandir.scandir_generic
    elif options.scandir == 'c':
        if scandir.scandir_c is None:
            print("ERROR: Compiled C version of scandir not found!")
            sys.exit(1)
        scandir.scandir = scandir.scandir_c
    elif options.scandir == 'python':
        if scandir.scandir_python is None:
            print("ERROR: Python version of scandir not found!")
            sys.exit(1)
        scandir.scandir = scandir.scandir_python
    elif options.scandir == 'os':
        if not hasattr(os, 'scandir'):
            print("ERROR: Python 3.5's os.scandir() not found!")
            sys.exit(1)
        scandir.scandir = os.scandir
    elif hasattr(os, 'scandir'):