天天看点

python 遍历目录或文件python 遍历目录或文件

python 遍历目录或文件

如果想得到某个目录下面的所有文件名,用 python 怎么做?

先看代码:

import os,sys

def explore(dir):
    for root, dirs, files in os.walk(dir):
        print('debug: ', root, dirs, files) # 这行用来调试,帮助理解代码    
        for file in files:
            path = os.path.join(root, file)
            print(path)

 
def main():
    for path in sys.argv[1:]:           
        if os.path.isdir(path):
            explore(path)
			
			
if __name__ == "__main__":
    main()
           

假设上面的代码保存为文件 get_dirs.py

我们实操一下。

当前目录结构如下:

$ tree
.
├── a
│   └── a1
│       └── a11
├── a.txt
├── b
├── b.txt
├── c.txt
├── get_dirs.py
└── ss
    └── s.txt

           

结果是:

$ python3 get_dirs.py ./
debug:  ./ ['a', 'b', 'ss'] ['a.txt', 'b.txt', 'c.txt', 'get_dirs.py']
./a.txt
./b.txt
./c.txt
./get_dirs.py
debug:  ./a ['a1'] []
debug:  ./a/a1 ['a11'] []
debug:  ./a/a1/a11 [] []
debug:  ./b [] []
debug:  ./ss [] ['s.txt']
./ss/s.txt
           

代码解读如下。

for root, dirs, files in os.walk(dir):
        print('debug: ', root, dirs, files) # 这行用来调试,帮助理解代码    
        for file in files:
            path = os.path.join(root, file)
            print(path)
           

第 1 行的 os.walk 的函数声明为:

walk(top, topdown=True, onerror=None, followlinks=False)

参数:

  • top 是你所要便利的目录的地址
  • topdown 为真,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为真)
  • onerror 需要一个 callable 对象,当 walk 需要异常时,会调用
  • followlinks 如果为真,则会遍历目录下的快捷方式(linux 下是 symbolic link)实际所指的目录(默认为假)

os.walk 的返回值是一个生成器(generator),也就是说我们需要不断地遍历它,以获得所有内容。

可以理解为 os.walk 会遍历所有的目录(不包括文件),每次遍历都会返回一个三元组:(root,dirs,files)。

  • root 表示当前正在遍历的目录
  • dirs 是一个 list ,会列出 root 下所有的目录(不包括子目录)
  • files 也是一个 list , 会列出 root 下所有的文件(不包括子目录里的文件)

知道了这些,你再对比上面的打印结果就会豁然开朗。

代码第 4 行,

path = os.path.join(root, file)

这句是什么意思呢?其实就是把 root 和 file 拼接到一起,组成完整的文件名。

举个例子就明白了。

>>> import os
>>> os.getcwd()
'/mnt/hgfs/vm_share/grep_test'
>>> my_path = os.path.join(os.getcwd(), "hello")
>>> print(my_path)
/mnt/hgfs/vm_share/grep_test/hello

           

参考资料

【1】python中os.walk的用法

【2】os.path.join()用法