Check if a file exists

From CodeCodex

Implementations[edit]

Erlang[edit]

file_exist(Filename) ->
    case file:read_file_info(Filename) of
        {ok, _}         -> io:format("~s is found~n", [Filename]);
        {error, enoent} -> io:format("~s is missing~n", [Filename]);
        {error, Reason} -> io:format("~s is ~s~n", [Filename, Reason])
    end.

Java[edit]

import java.io.*;

public class FileTest {
  public static void main(String args[]) {
    File f = new File(args[0]);
    System.out.println
      (f + (f.exists()? " is found " : " is missing "));
  }
}

Ruby[edit]

def file_exist(fname)
  puts fname + (File.exist?(fname) ? " is found " : " is missing ")
end

Python[edit]

import os

def file_exists(file_name):
    if os.path.exists(file):
        return '%s is found' % file_name
    else:
        return '%s is missing' % file_name