收集的多个ruby遍历文件夹代码实例

   这篇文章主要介绍了收集的多个ruby遍历文件夹代码实例,本文总结了4个代码片段,小编推荐最后一个方法,因为它很简洁优雅,需要的朋友可以参考下

  一、遍历文件夹下所有文件,输出文件名

   代码如下:

  def traverse_dir(file_path)

  if File.directory? file_path

  Dir.foreach(file_path) do |file|

  if file !="." and file !=".."

  traverse_dir(file_path+"/"+file)

  end

  end

  else

  puts "File:#{File.basename(file_path)}, Size:#{File.size(file_path)}"

  end

  end

  traverse_dir('D:/apache-tomcat')

  二、ruby遍历文件夹

   代码如下:

  def get_file_list(path)

  Dir.entries(path).each do |sub|

  if sub != '.' && sub != '..'

  if File.directory?("#{path}/#{sub}")

  puts "[#{sub}]"

  get_file_list("#{path}/#{sub}")

  else

  puts " |--#{sub}"

  end

  end

  end

  end

  三、python如何遍历一个目录输出所有文件名

   代码如下:

  #coding=utf-8

  '''

  Created on 2014-11-14

  @author: Neo

  '''

  import os

  def GetFileList(dir, fileList):

  newDir = dir

  if os.path.isfile(dir):

  fileList.append(dir.decode('gbk'))

  elif os.path.isdir(dir):

  for s in os.listdir(dir):

  #如果需要忽略某些文件夹,使用以下代码

  #if s == "xxx":

  #continue

  newDir=os.path.join(dir,s)

  GetFileList(newDir, fileList)

  return fileList

  list = GetFileList('D:workspacePyDemofas', [])

  for e in list:

  print e

  result:

   代码如下:

  D:workspacePyDemofasfile120141113a.20141113-1100.log

  D:workspacePyDemofasfile120141113a.20141113-1101.log

  D:workspacePyDemofasfile120141113a.20141113-1140.log

  D:workspacePyDemofasfile220141113a.20141113-1100.log

  D:workspacePyDemofasfile220141113a.20141113-1101.log

  D:workspacePyDemofasfile220141113a.20141113-1140.log

  四、简洁遍历写法

   代码如下:

  import os

  def iterbrowse(path):

  for home, dirs, files in os.walk(path):

  for filename in files:

  yield os.path.join(home, filename)

  for fullname in iterbrowse("/home/bruce"):

  print fullname

时间: 2024-08-05 11:59:02

收集的多个ruby遍历文件夹代码实例的相关文章

收集的多个ruby遍历文件夹代码实例_ruby专题

一.遍历文件夹下所有文件,输出文件名 复制代码 代码如下: def traverse_dir(file_path)     if File.directory? file_path         Dir.foreach(file_path) do |file|             if file !="." and file !=".."                 traverse_dir(file_path+"/"+file)   

Ruby遍历文件夹同时计算文件的md5sum

  这篇文章主要介绍了Ruby遍历文件夹同时计算文件的md5sum,本文直接给出实现代码,需要的朋友可以参考下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #!/usr/bin/ruby -w # require 'digest/md5'   if ARGV.empty? puts "usgae: #$0 path" exi

Ruby遍历文件夹同时计算文件的md5sum_ruby专题

#!/usr/bin/ruby -w # require 'digest/md5' if ARGV.empty? puts "usgae: #$0 path" exit 0 end dir_name=ARGV.shift def dir_md5sum(path) md5s=Array.new if File.directory?(path) Dir.new(path).each do |file| next if file =~ /^\.+$/ file="#{path}/#

Ruby常用文件操作代码实例

  这篇文章主要介绍了Ruby常用文件操作代码实例,如新建文件.输出文件内容.IO操作.输出文件路径.stringio使用等内容,需要的朋友可以参考下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6

Lua中遍历文件操作代码实例

  这篇文章主要介绍了Lua中遍历文件操作代码实例,本文直接给出示例代码,需要的朋友可以参考下 写的一个关于遍历文件的程序段 记录一下咯 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 --[[检查所有.txt文件 比如A.txt中第一行规定有20列,但是在X行中多输入一个Tab,则输出:A表的X行填写不规范,行末有多余填写 ]]   getinfo = io.popen('dir .

Lua中遍历文件操作代码实例_Lua

写的一个关于遍历文件的程序段  记录一下咯 --[[检查所有.txt文件 比如A.txt中第一行规定有20列,但是在X行中多输入一个Tab,则输出:A表的X行填写不规范,行末有多余填写 ]] getinfo = io.popen('dir ..//file /b /s') all = getinfo:read('*all') local filenameList = io.open("filename.txt", "wb") filenameList:write(&

PHP遍历文件夹与文件类及处理类用法实例

  本文实例讲述了PHP遍历文件夹与文件类及处理类用法,非常具有实用价值.分享给大家供大家参考.具体方法如下: FindFile.class.php类文件用于遍历目录文件,具体代码如下: <?php /** 遍历文件夹及文件类 * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */ class FindFile{ public $files = array(); // 存储遍历的文件 protected $maxdepth; // 搜寻深度,0表示

PHP遍历文件夹及文件类及处理类

FindFile.class.php 用于遍历目录文件 <?php /** 遍历文件夹及文件类 * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */ class FindFile{ public $files = array(); // 存储遍历的文件 protected $maxdepth; // 搜寻深度,0表示没有限制 /* 遍历文件及文件夹 * @param String $spath 文件夹路径 * @param int $maxdep

ASP.NET2.0 遍历文件夹下所有图片

asp.net|遍历|遍历文件 1.以下目录有若干图片(都是大图片)2.在页面展现效果图 3.代码 后台代码using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;us