一、新建文件
f=file.new(file.join("c:","test.txt"), "w+")
f.puts("i am jack")
f.puts("hello world")
文件模式
"r" :read-only. starts at beginning of file (default mode).
"r+" :read-write. starts at beginning of file.
"w" :write-only. truncates existing file to zero length or creates a new file for writing.
"w+" :read-write. truncates existing file to zero length or creates a new file for reading and writing.
"a" :write-only. starts at end of file if file exists; otherwise, creates a new file for writing.
"a+" :read-write. starts at end of file if file exists; otherwise, creates a new file for reading and writing.
"b" :(dos/windows only.) binary file mode. may appear with any of the key letters listed above
二、读取文件
file=file.open(file.join("c:","test.txt"),"r")
file.each { |line| print "#{file.lineno}.", line }
file.close
三、新建、删除、重命名文件
file.new( "books.txt", "w" )
file.rename( "books.txt", "chaps.txt" )
file.delete( "chaps.txt" )
四、目录操作
1 创建目录
dir.mkdir("c:/testdir")
#删除目录
dir.rmdir("c:/testdir")
#查询目录里的文件
p dir.entries(file.join("c:","ruby")).join(' ')
#遍历目录
dir.entries(file.join("c:","ruby")).each {
|e| puts e
}
1、argv and argf
argv
argv << "cnblogslink.txt"
#the gets method is a kernel method that gets lines from argv
print while gets
p argv.class
argf
while line = argf.gets
print line
end
2、文件信息查询
#文件是否存在
p file::exists?( "cnblogslink.txt" ) # => true
#是否是文件
p file.file?( "cnblogslink.txt" ) # => true
#是否是目录
p file::directory?( "c:/ruby" ) # => true
p file::directory?( "cnblogslink.txt" ) # => false
#文件权限
p file.readable?( "cnblogslink.txt" ) # => true
p file.writable?( "cnblogslink.txt" ) # => true
p file.executable?( "cnblogslink.txt" ) # => false
#是否是零长度
p file.zero?( "cnblogslink.txt" ) # => false
#文件大小 bytes
p file.size?( "cnblogslink.txt" ) # => 74
p file.size( "cnblogslink.txt" ) # => 74
#文件或文件夹
p file::ftype( "cnblogslink.txt" ) # => "file"
#文件创建、修改、最后一次存取时间
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
3、查找文件
puts "查找目录下所有文件及文件夹"
dir["c:/ruby/*"].each {|x|
puts x
}
puts "条件查询"
dir.foreach('c:/ruby') {
|x| puts x if x != "." && x != ".."
}
puts "查找某一类型文件"
dir["*.rb"].each {|x|
puts x
}
puts "open 查询"
dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}
puts "---------------------------"
puts "正则表达式查询"
dir["c:/ruby/ruby/[rs]*"].each{|x| puts x}
puts "------------------------"
dir["c:/ruby/[^s]*"].each{|x| puts x}
puts "------------------------"
dir["c:/ruby/{ruby,li}*"].each{|x| puts x}
puts "------------------------"
dir["c:/ruby/?b*"].each{|x| puts x}
puts "查找目录及子目录的文件"
require 'find'
find.find('./') { |path| puts path }
3、查询目录及子目录文件
require "find"
find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|
find.prune if f == "."
puts f
end
原型:ref.find( [ aname ]* ) {| afilename | block }
prune:skips the current file or directory, restarting the loop with the next entry. if the current file is a directory, that directory will not be recursively entered. meaningful only within the block associated with find::find.
4、文件比较 复制等
require 'ftools'
file.copy 'testfile', 'testfile1' » true
file.compare 'testfile', 'testfile1' » true
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
循序渐进掌握递归正则表达式
一般来说,递归的正则表达式用来匹配任意嵌套层次的结构或左右对称的结构。例如匹配: 递归正则在正则表达式里算是比较灵活的部分,换句话说就是可能会比较难...
[阅读全文]
-
scrapy 使用
启动方式: 写一个启动文件,与配置文件同级 from scrapy.cmdline import execute import sys,os sys...
[阅读全文]
-
-
-
经典例题
例题: 1.用户输入账号 2.用户输入密码 3.判断用户的账号是不是alex 4.如果账号是alex在继续判断密码是不是alexdsb 5.账号和密...
[阅读全文]
-
-
-
-
-
网友评论