最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

收集的多個(gè)ruby遍歷文件夾代碼實(shí)例

 更新時(shí)間:2015年05月22日 10:19:34   投稿:junjie  
這篇文章主要介紹了收集的多個(gè)ruby遍歷文件夾代碼實(shí)例,本文總結(jié)了4個(gè)代碼片段,小編推薦最后一個(gè)方法,因?yàn)樗芎?jiǎn)潔優(yōu)雅,需要的朋友可以參考下

一、遍歷文件夾下所有文件,輸出文件名

復(fù)制代碼 代碼如下:

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遍歷文件夾

復(fù)制代碼 代碼如下:

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如何遍歷一個(gè)目錄輸出所有文件名

復(fù)制代碼 代碼如下:

#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:\\workspace\\PyDemo\\fas', [])
for e in list:
    print e

result:

復(fù)制代碼 代碼如下:

D:\workspace\PyDemo\fas\file1\20141113\a.20141113-1100.log
D:\workspace\PyDemo\fas\file1\20141113\a.20141113-1101.log
D:\workspace\PyDemo\fas\file1\20141113\a.20141113-1140.log
D:\workspace\PyDemo\fas\file2\20141113\a.20141113-1100.log
D:\workspace\PyDemo\fas\file2\20141113\a.20141113-1101.log
D:\workspace\PyDemo\fas\file2\20141113\a.20141113-1140.log

四、簡(jiǎn)潔遍歷寫(xiě)法

復(fù)制代碼 代碼如下:

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

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

定边县| 安阳市| 兰溪市| 保康县| 福州市| 汝州市| 霍林郭勒市| 永吉县| 甘孜县| 巴彦淖尔市| 中阳县| 梅州市| 安福县| 宜丰县| 宜阳县| 津南区| 伊通| 雷波县| 西吉县| 临猗县| 新建县| 安国市| 田阳县| 澄城县| 嘉禾县| 广宁县| 仁寿县| 新乡市| 沙田区| 连平县| 蒙山县| 鄂温| 金门县| 桦甸市| 宁德市| 岳普湖县| 北碚区| 徐州市| 太和县| 汝州市| 会泽县|