One-step learning of Ruby (21): File Operations 2

Source: Internet
Author: User

1. Open and read files

 
File = file. Open ("cnblogslink.txt") file. Each {| Line | print "# {file. lineno}.", line} file. Close

Output:

1.Community

2. News

3. Community

4. News

5. Recruitment

6. Bo Wen

7. Group
8. Flash
9. Network Abstract
10.. Net Channels

File. lineno displays the row number.

2. argv and argf

Argv

 
Argv <"cnblogslink.txt" # The gets method is a kernel method that gets lines from argvprint while getsp argv. class = begin output: Community news community news recruitment Q & A Team Flash network Abstract. net channel Array = end

Argf

Let's write the following in test. RB:Code:

While line = argf. Gets print lineend

Run the following command in the command line:

 
C: \ studyruby> test. RB cnblogslink.txt cnblogslink2.txt community news recruitment blog Q & A Team Flash network extraction. Net channel Community news community news recruitment blog Q & A Team Flash network extraction. Net Channel

3. File Information Query

# Does the file have P file: exists? ("Cnblogslink.txt") # => true # Is it a file P file. file? ("Cnblogslink.txt") # => true # Is it the directory P file: directory? ("C:/ruby") # => truep file: directory? ("Cnblogslink.txt") #=> false # File Permission P file. readable? ("Cnblogslink.txt") # => truep file. writable? ("Cnblogslink.txt") # => truep file.exe cutable? ("Cnblogslink.txt") # => false # Is it a zero-length P file. Zero? ("Cnblogslink.txt") # => false # file size bytesp file. Size? ("Cnblogslink.txt") # => 74 P file. size ("cnblogslink.txt") # => 74 # file or folder P file: FTYPE ("cnblogslink.txt ") # => "file" # file creation, modification, and last access time P file: ctime ("cnblogslink.txt") # => sat Sep 19 08:05:07 + 0800 2009 P file :: mtime ("cnblogslink.txt") # => sat Sep 19 08:06:34 + 0800 2009 P file: atime ("cnblogslink.txt") # => sat Sep 19 08:05:07 + 0800 2009

4. Search for files

Puts "Find all files and folders in the directory" dir ["C:/Ruby/*"]. each {| x | puts x} puts "condition query" dir. foreach ('C:/Ruby ') {| x | puts X if X! = "." & X! = ".. "} Puts" Find a type of file "dir ["*. RB "]. each {| x | puts x} puts "Open query" dir. open ('C:/Ruby ') {| d | D. grep/L /}. each {| x | puts x} puts "---------------------------" dir. open ('C:/Ruby ') {| d | D. each {| x | puts x} puts "Regular Expression query" dir ["C:/Ruby/[RS] *"]. each {| x | puts x} puts "----------------------" dir ["C:/Ruby/[^ s] *"]. each {| x | puts x} puts "----------------------" dir ["C:/Ruby/{Ruby, Li} *"]. each {| x | Pu TS x} puts "------------------------" dir ["C:/Ruby /? B * "]. each {| x | puts x} puts "find the Directory and subdirectory file" require 'Find 'find. find ('. /') {| path | puts path}

The output is as follows:

> Ruby test. find all files and folders in the directory c:/Ruby/binc:/Ruby/changelog.txt C:/Ruby/docc:/Ruby/libc:/Ruby/license.txt C: /Ruby/manc:/Ruby/manifestc:/Ruby/miscc:/Ruby/readme.1stc:/Ruby/releasenotes.txt C:/Ruby. ICOC:/Ruby/rubyopt. DELC:/Ruby/rubyw. ICOC:/Ruby/samplesc:/Ruby/scitec:/Ruby/sharec:/Ruby/SRCC:/Ruby/example find a type of file test. rbtest2.rbopen contains regular expression query ---------------------- C:/Ruby/binc:/Ruby/changelog.txt C:/Ruby/docc:/Ruby/libc:/Ruby/license.txt C:/Ruby/manc: /Ruby/manifestc:/Ruby/miscc:/Ruby/readme.1stc:/Ruby/releasenotes.txt C:/Ruby. ICOC:/Ruby/rubyopt. DELC:/Ruby/rubyw. ICOC:/Ruby/uninstall.exe ---------------------- C:/Ruby. ICOC:/Ruby/rubyopt. DELC:/Ruby/rubyw. ICOC:/Ruby/libc:/Ruby/license.txt ------------------------ find the file in the subdirectory of the directory. /. /test2.rb. /test2./Test2/test2.rb. /Test2/test. RB. /test1./test. RB. /output. /films.txt. /cnblogslink2.txt. /cnblogslink.txt. /beans.txt> exit code: 0
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.