初学Ruby,很多需要学习,现在开始尝试使用Ruby来写一个脚本,其中用到了很多文件相关的操作,这里阶段地整理一些。便于后续的再次查找。
1 | File.exist?('file_path') |
---|
1 | File.file?("file_path") |
---|
1 | File.directory?("file_path") |
---|
1 2 3 4 5 6 7 | File.basename('/tmp/adb.log') #=> "adb.log" #从上面结果中移除扩展名 File.basename('/tmp/adb.log', '.log') #=> "adb" #或者 File.basename('/tmp/adb.log', '.*') #=> "adb" |
---|
1 2 | #替换puts child为自己的操作 Dir['/tmp/*'].each{|child|puts child} |
---|
1 2 3 4 5 6 | #特定目录的父路径 File.expand_path("..",specific_path) #当前目录的父路径 File.expand_path("..",Dir.pwd) #或者 File.expand_path("..") |
---|