Ruby文件操作

获取不带目录的文件名(带有后缀)

File.basename( "foo/bar/jim-jam/whee.html" )       #=> "whee.html"
获取文件名(不带后缀)

File.basename( "foo/bar/jim-jam/whee.html", ".*" ) #=> "whee"

获取同一个目录下不同后缀的文件

filename  = File.basename(my_path,".*")
othername = File.join( File.dirname(my_path), "#{filename}.jpg" )

或者

othername = my_path.sub(/[^.]+\z/,"jpg")  # replace non-period characters at the end of the string with 'jpg'

参考:


http://ruby-doc.org/core-2.2.0/File.html

http://phrogz.net/filename-without-extension-ruby

http://stackoverflow.com/questions/23356777/ruby-get-filename-without-the-extensions

http://stackoverflow.com/questions/374326/get-filename-without-extension-from-file-path-in-ruby

irb(main):024:0> f = 'C:\foobar\blah.txt'.gsub("\\","/")
=> "C:/foobar/blah.txt"
irb(main):027:0> File.basename(f,File.extname(f))
=> "blah"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值