- 함수 file_exists()
- 함수 dir_exists()
- Checks whether a file or directory exists
1 Bash[ | ]

Bash
Copy
filename='/etc/hosts'
if [ -f $filename ]; then
echo "'$filename' exists."
else
echo "'$filename' does not exist."
fi
Bash
Copy
if [ ! -f $filename ]; then
echo "'$filename' does not exist."
fi
2 CMD[ | ]
bat
Copy
if exist "C:\Windows\Setup.log" echo exists.
REM exists.
if not exist "C:\Windows\Setup2.log" echo not exists.
REM not exists.
bat
Copy
if exist "C:\Windows\" echo exists.
REM exists.
if not exist "C:\Windows2\" echo not exists.
REM not exists.
3 C#[ | ]
C#
Copy
string filename = @"C:\Users\Public\DeleteTest\test.txt";
if(System.IO.File.Exists(filename)) {
Console.WriteLine(filename + " exists.");
} else {
Console.WriteLine(filename + " does not exists.");
}
4 Go[ | ]

Go
Copy
package main
import (
"fmt"
"os"
)
func FileExists(filename string) bool {
info, err := os.Stat(filename)
return err == nil && !info.IsDir()
}
func main() {
// true
fmt.Println(FileExists("/etc/services")) // File Exists
// false
fmt.Println(FileExists("/etc/file-not-exists")) // File Not Exists
fmt.Println(FileExists("/etc")) // Directory Exists
}
Loading
5 Java[ | ]

Java
Copy
System.out.println( new File("C:\\Windows\\win.ini").exists() );
System.out.println( new File("C:\\not-exist-file").exists() );
// true
// false
6 PHP[ | ]

PHP
Copy
var_dump( file_exists("/etc/hosts") );
var_dump( file_exists("/tmp/not-exist-file") );
# bool(true)
# bool(false)
7 Python[ | ]
Python
Copy
import os.path
filename = '/etc/passwd'
if os.path.isfile(filename):
print( filename + ' exists.' )
else:
print( filename + ' does not exist.' )
8 Perl[ | ]
Perl
Copy
my $filename = '/etc/passwd';
if ( -f $filename ) {
print $filename . " exists.";
} else {
print $filename . " does not exist.";
}