遍历目录树的所有方法是什么? [英] What are all the ways to traverse directory trees?

查看:142
本文介绍了遍历目录树的所有方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在不同的操作系统中遍历一个目录树需要知道些什么?在不同的文件系统上?



你最喜欢的库/模块是用来帮助遍历一个目录树吗?

解决在 Python 中:



如果您正在寻找一个快速,干净且便携的解决方案,请尝试: / p>

  import os 
base_dir ='。'

def foo(arg,curr_dir,files ):
print curr_dir
打印文件
$ b $ os.path.walk(base_dir,foo,None)

请注意,您可以修改foo来执行其他操作,而不仅仅是打印名称。此外,如果您有兴趣迁移到Python 3.0,则必须使用os.walk()。

How do you traverse a directory tree in your favorite language?

What do you need to know to traverse a directory tree in different operating systems? On different filesystems?

What's your favorite library/module for aiding in traversing a directory tree?

解决方案

In Python:

If you're looking for a quick, clean, and portable solution try:

import os
base_dir = '.'

def foo(arg, curr_dir, files):
  print curr_dir
  print files

os.path.walk(base_dir, foo, None)

Note that you can modify foo to do something else instead of just printing the names. Furthermore, if you're interested in migrating to Python 3.0, you will have to use os.walk() instead.

这篇关于遍历目录树的所有方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆