真的很奇怪,当我使用 File.exist 时?或 File.exists?我发现它不区分大小写?
2.7.0-preview1 :001 > Dir.entries('.')
=> [".", "..", "ppp", "FOO", "Bar"]
2.7.0-preview1 :002 > File.exist? "foo"
=> true
2.7.0-preview1 :003 > File.exist? "FOO"
=> true
2.7.0-preview1 :004 > File.exist? "FOOBAR"
=> false
2.7.0-preview1 :005 > File.exists? "FOO"
=> true
2.7.0-preview1 :006 > File.exists? "foo"
=> true
Run Code Online (Sandbox Code Playgroud)
我如何做区分大小写的 File.exist?? 我使用的是 macOS Catalina 10.15.3
更新
对于@Stefan 问题:我为什么要问这个问题,我只是在练习本书中的代码片段 - Ruby Cookbook ver.2,配方是批量重命名文件,如下所示:
require 'find'
module Find
def rename(*paths)
unrenamable = []
find(*paths) do |file|
next unless File.file? file # skip directory
path, name = File.split(file)
new_name = yield name
if new_name && new_name != name
new_path = File.join(path, new_name)
if File.exist? new_path
unrenamable << file
else
puts "Renaming #{file} to #{new_path}" if $DEBUG
File.rename(file, new_path)
end
end
end
unrenamable
end
module_function(:rename)
end
Run Code Online (Sandbox Code Playgroud)
第一个用例是将所有文件名转换为小写
File.rename('./') { |f| f.downcase }
Run Code Online (Sandbox Code Playgroud)
该生产线if File.exist? new_path如果old_path和new_path是上或下的情况下只是不同的是事实,那么所有的文件都是“unrenamable”
我如何做区分大小写的 File.exist?
使用区分大小写的文件系统。
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |