Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a quick way to test to see a file exists?

I want to quickly check to see if a file exists in my iPhone app's Documents directory (or any path for that matter). I can enumerate through the directory's files, or I can try to open a specific file. What's the fastest way? I just need to know if the file is there or if it does not exist.

like image 281
mahboudz Avatar asked Aug 31 '09 09:08

mahboudz


People also ask

How do I check to see if a file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

Which method is used to check file existence?

The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false.

How do I check whether a file exists without exceptions?

To check whether a Path object exists independently of whether is it a file or directory, use my_path. exists() .

How check file exist in Python?

In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.


1 Answers

Swift v3:

let fileExists = FileManager.default.fileExists(atPath: somePath)

Thanks to Nikolay Suvandzhiev.

Objective-C (Original):

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath];

like image 61
rein Avatar answered Oct 12 '22 23:10

rein