Multiple ruby traversal folder code instances collected, ruby instances

Source: Internet
Author: User

Multiple ruby traversal folder code instances collected, ruby instances

1. traverse all files in the folder and output file names

Copy codeThe Code is as follows:
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 ')

2. ruby traversal of folders

Copy codeThe Code is as follows:
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

3. How does python traverse a directory and output all file names?

Copy codeThe Code is as follows:
# 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 ):
# Use the following code to ignore some folders:
# 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:
Copy codeThe Code is as follows:
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

Iv. Simple Traversal
Copy codeThe Code is as follows:
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.