Beginners Ruby, many need to learn, now start to try to write a script using Ruby, which uses a lot of file-related operations, here to organize some. Easy to find again later.
Whether a file or directory exists
Copy Code code as follows:
File.exist? (' File_path ')
is a file
Copy Code code as follows:
is a directory
Copy Code code as follows:
File.directory? ("File_path")
Get the file name from the path
Copy Code code as follows:
File.basename ('/tmp/adb.log ') #=> "Adb.log"
#从上面结果中移除扩展名
File.basename ('/tmp/adb.log ', '. Log ') #=> "adb"
#或者
File.basename ('/tmp/adb.log ', '. * ') #=> "adb"
List all the subfolders under the directory
Copy Code code as follows:
#替换puts child for their own operation
dir['/tmp/* '].each{|child|puts Child}
Get Parent Directory
Copy Code code as follows:
#特定目录的父路径
File.expand_path ("..", Specific_path)
#当前目录的父路径
File.expand_path ("..", Dir.pwd)
#或者
File.expand_path ("..")