python 目录 子文件夹
python list sub folder file
Python: List all the file names in a directory and its subdirectories and then print the results in a txt file – Stack Overflow
Python : How to get list of files in directory and sub directories – thispointer.com
python os.listdir vs os.walk
“scandir has been included in the Python 3.5 standard library as os.scandir(), ”
“from os import scandir, walk”
python – Should glob.glob(…) be preferred over os.listdir(…) or the other way around? – Stack Overflow
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.
path may be a path-like object. If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str.
This function can also support specifying a file descriptor; the file descriptor must refer to a directory.
To encode str filenames to bytes, use fsencode().
The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.
Changed in version 3.2: The path parameter became optional.
New in version 3.3: Added support for specifying an open file descriptor for path.
Changed in version 3.6: Accepts a path-like object.”
Return an iterator of os.DirEntry objects corresponding to the entries in the directory given by path. The entries are yielded in arbitrary order, and the special entries ‘.’ and ‘..’ are not included.
Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type or file attribute information, because os.DirEntry objects expose this information if the operating system provides it when scanning a directory. “
import os import logging OutputFolder = "/Users/crifan/dev/tmp/xxx_downloadDemo/output" def processVideo(): userFolder = os.path.join(OutputFolder, "user") logging.info("userFolder=%s", userFolder) with os.scandir(userFolder) as dirEntryList: for curDirEntry in dirEntryList: logging.info("curDirEntry=%s", curDirEntry) # <DirEntry '432824'> if curDirEntry.is_dir(): dirName = curDirEntry.name logging.info("dirName=%s", dirName) # 432824 else: logging.warn("Omit non-dir: %s", curDirEntry.name)