首页 > 编程语言 >Python: recurse directory

Python: recurse directory

时间:2022-11-10 12:02:16浏览次数:41  
标签:Python top recurse st mode file directory path os

 

import os, sys
from stat import *
from typing import Callable


def visit_file(file):
    print('visiting', file)


def walktree(top, callback: Callable):
    print(f'{top=}')
    for f in os.listdir(top):
        path = os.path.join(top, f)
        st_mode = os.stat(path, follow_symlinks=False).st_mode

        if S_ISDIR(st_mode):
            walktree(path, callback)
        elif S_ISREG(st_mode):
            callback(path)
        else:
            print('skipping %s' % path)


walktree('log_', visit_file)

 

标签:Python,top,recurse,st,mode,file,directory,path,os
From: https://www.cnblogs.com/dissipate/p/16876606.html

相关文章