What are all the ways to traverse directory trees?

后端 未结 7 1072
挽巷
挽巷 2021-01-02 04:13

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 f

7条回答
  •  感情败类
    2021-01-02 04:16

    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.

提交回复
热议问题