- 함수 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
CPU
-1.0s
MEM
-0M
-1.0s
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
}
true false false
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.";
}
9 같이 보기[ | ]
10 참고[ | ]
편집자 Jmnote Jmnote bot Ykhwong
로그인하시면 댓글을 쓸 수 있습니다.
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ―Pinkcrimson
유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong