traverse-directories


目录遍历

下面整理常见的目录遍历方式:

目录结构为:

traverse-directories
    sell.txt
    fuit-shop\
        orange.txt
        apple.txt
    car\
        small-car\
            奔驰.txt
        big-car\
            拖拉机.txt

os.listdir

返回当前目录文件或目录名列表


In [10]:
import os
os.listdir('traverse-directories')


Out[10]:
['car', 'fuit-shop', 'sell.txt']

os.scandir

返回可迭代的对象os.DirEntry ,可以直接判断是文件还是目录等.


In [11]:
with os.scandir('traverse-directories') as it:
    for entry in it:
        print(entry.name,entry.is_file())


car False
fuit-shop False
sell.txt True

os.walk

可以递归遍历目录, 另外还支持遍历符号链接指向的目录.


In [14]:
for root, dirs, files in os.walk("traverse-directories"):
    for d in dirs:
        print (os.path.join(root,d))

    for f in files:
        print (os.path.join(root,f))


traverse-directories\car
traverse-directories\fuit-shop
traverse-directories\sell.txt
traverse-directories\car\big-car
traverse-directories\car\small-car
traverse-directories\car\big-car\拖拉机.txt
traverse-directories\car\small-car\奔驰.txt
traverse-directories\fuit-shop\apple.txt
traverse-directories\fuit-shop\orange.txt

glob.glob

如果文件比较多的话可以使用glob.iglob提高性能.


In [50]:
import glob
print('普通遍历:')
for i in glob.glob('traverse-directories/*'):
    print(i)
print('递归遍历:')
for i in glob.glob('traverse-directories/**',recursive=True):
    print(i)


普通遍历:
traverse-directories\car
traverse-directories\fuit-shop
traverse-directories\sell.txt
递归遍历:
traverse-directories\
traverse-directories\car
traverse-directories\car\big-car
traverse-directories\car\big-car\拖拉机.txt
traverse-directories\car\small-car
traverse-directories\car\small-car\奔驰.txt
traverse-directories\fuit-shop
traverse-directories\fuit-shop\apple.txt
traverse-directories\fuit-shop\orange.txt
traverse-directories\sell.txt

pathlib.Path

自从python3开始,有了Path 这个表示路径的对象,写起来很面向对象.


In [25]:
from pathlib import Path
root = Path('traverse-directories')
print("遍历目录方式一:")
for child in root.iterdir():
    print (child)
print("遍历目录方式二:")
for i in root.glob('*'):
    print(i)
print("遍历目录方式三(递归):")
for i in root.glob('**/*'):
    print(i)


遍历目录方式一:
traverse-directories\car
traverse-directories\fuit-shop
traverse-directories\sell.txt
遍历目录方式二:
traverse-directories\car
traverse-directories\fuit-shop
traverse-directories\sell.txt
遍历目录方式三(递归):
traverse-directories\car
traverse-directories\fuit-shop
traverse-directories\sell.txt
traverse-directories\car\big-car
traverse-directories\car\small-car
traverse-directories\car\big-car\拖拉机.txt
traverse-directories\car\small-car\奔驰.txt
traverse-directories\fuit-shop\apple.txt
traverse-directories\fuit-shop\orange.txt