现在的位置: 首页 > 综合 > 正文

递归遍历文件夹脚本

2013年10月11日 ⁄ 综合 ⁄ 共 1164字 ⁄ 字号 评论关闭

递归遍历某个文件夹下的所有文件是一项很常见的操作,总结通过shell和Python来递归遍历文件夹的脚本如下。

  1.  shell version
    #!/bin/bash
     
    function walk()
    {
      for file in `ls $1`
      do
        local path=$1"/"$file
        if [ -d $path ]
         then
          echo "DIR $path"
          walk $path
        else
          echo "FILE $path"
        fi
      done
    }
     
    if [ $# -ne 1 ]
    then
      echo "USAGE: $0 TOP_DIR"
    else
      walk $1
    fi

  2. Python version 1, using os.listdir
    def os_list_dir(top_dir):
      for file in os.listdir(top_dir):
        file_path = os.path.abspath(os.path.join(top_dir, file))
        if os.path.isfile(file_path):
          print 'FILE', file_path
        elif os.path.isdir(file_path):
          print 'DIR', file_path
          os_list_dir(file_path)

  3. Python version 2, using os.walk
    def os_walk(top_dir):
      for parent, dirnames, filenames in os.walk(top_dir):
        for filename in filenames:
          print 'FILE', os.path.abspath(os.path.join(parent, filename))
        for dirname in dirnames:
          print 'DIR', os.path.abspath(os.path.join(parent, dirname))
        #del dirnames[:]  #uncomment this line to not walk recursively

  4. Python version 3, using os.path.walk
    def os_path_walk(top_dir):
      def print_name(arg, dirname, files):
        for file in files:
          file_path = os.path.abspath(os.path.join(dirname, file))
          if os.path.isfile(file_path):
            print 'FILE', file_path
          elif os.path.isdir(file_path):
            print 'DIR', file_path
        #del files[:] #uncomment this line to not walk recursively
     
      os.path.walk(top_dir, print_name, None)

本文出自夜惊心的博客,转载请保留出处

抱歉!评论已关闭.