概要
File.exists?
詳細
ファイルまたはディレクトリが存在するか判定する。
サンプル
コード
カレントディレクトリがWindowsのC:\tmpだったとします。
# encoding: utf-8 def create_file(filename, contents) File.open(filename, 'w:utf-8') do |f| f.print contents end end filenames = ['sample1.txt', 'sample2.txt'] p File.exists? 'tmp' p File.exists? 'sample1.txt' Dir.mkdir('tmp') unless File.exists?('tmp') filenames.each {|f|create_file f, "test"} p File.exists? 'tmp' p File.exists? 'sample1.txt'
出力
false false true true