Answers for "python scan directory"

5

how to search for a specific file extension with python

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)
Posted by: Guest on March-06-2020
2

python get dir

import os 
#full path
dir_path = os.path.dirname(os.path.realpath(__file__))

#current dir
cwd = os.getcwd()
Posted by: Guest on December-25-2020
0

find directory in directory python

import os

def listdir_r(dirpath):
  	"""lists directories and sub-directories recursively. 
    """
    paths=[]
    paths.append(dirpath)
    for path in os.listdir(dirpath):
        rpath = os.path.join(dirpath, path)
        if os.path.isdir(rpath):
            subdirs = listdir_r(rpath)
            if not subdirs == []:
                paths.extend(subdirs)
    return paths

# print the list of sub-directories
print(listdir_r('./'))
Posted by: Guest on June-09-2021
0

scan all directories with python

import glob

# path to search single character filename
path = "sales/?.jpeg"
for file in glob.glob(path):
    print(file)

# path to search three-character filename
path = "sales/???.jpeg"
for file in glob.glob(path):
    print(file)

# search file that starts with word 'cha' followed by exact two-character
path = "sales/cha??.txt"
for file in glob.glob(path):
    print(file)
Posted by: Guest on August-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language