How to Check for File Existence

What's the best way to check if a file exists in C?

Look up the access() function, found in unistd.h. You can replace your function with

if (access(fname, F_OK) == 0) {
// file exists
} else {
// file doesn't exist
}

Under Windows (VC) unistd.h does not exist. To make it work it is necessary to define:

#ifdef WIN32
#include <io.h>
#define F_OK 0
#define access _access
#endif

You can also use R_OK, W_OK, and X_OK in place of F_OK to check for read permission, write permission, and execute permission (respectively) rather than existence, and you can OR any of them together (i.e. check for both read and write permission using R_OK|W_OK)

Update: Note that on Windows, you can't use W_OK to reliably test for write permission, since the access function does not take DACLs into account. access( fname, W_OK ) may return 0 (success) because the file does not have the read-only attribute set, but you still may not have permission to write to the file.

How to check if a file exists in a shell script

You're missing a required space between the bracket and -e:

#!/bin/bash
if [ -e x.txt ]
then
echo "ok"
else
echo "nok"
fi

Fastest way to check if a file exists using standard C++/C++11,14,17/C?

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn't.

#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>

inline bool exists_test0 (const std::string& name) {
ifstream f(name.c_str());
return f.good();
}

inline bool exists_test1 (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}

inline bool exists_test2 (const std::string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}

inline bool exists_test3 (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}

Results for total time to run the 100,000 calls averaged over 5 runs,



























MethodTime
exists_test0 (ifstream)0.485s
exists_test1 (FILE fopen)0.302s
exists_test2 (posix access())0.202s
exists_test3 (posix stat())0.134s

How can I check if a file exists in python?

I think you just need that

try:
f = open('myfile.xlxs')
f.close()
except FileNotFoundError:
print('File does not exist')

If you want to check with if-else than go for this:

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
# file exists

How do I check if file exists in Makefile so I can delete it?

The second top answer mentions ifeq, however, it fails to mention that this ifeq must be at the same indentation level in the makefile as the name of the target, e.g., to download a file only if it doesn't currently exist, the following code could be used:

download:
ifeq (,$(wildcard ./glob.c))
curl … -o glob.c
endif

# THIS DOES NOT WORK!
download:
ifeq (,$(wildcard ./glob.c))
curl … -o glob.c
endif

How to check if a file exists in Go?

To check if a file doesn't exist, equivalent to Python's if not os.path.exists(filename):

if _, err := os.Stat("/path/to/whatever"); errors.Is(err, os.ErrNotExist) {
// path/to/whatever does not exist
}

To check if a file exists, equivalent to Python's if os.path.exists(filename):

Edited: per recent comments

if _, err := os.Stat("/path/to/whatever"); err == nil {
// path/to/whatever exists

} else if errors.Is(err, os.ErrNotExist) {
// path/to/whatever does *not* exist

} else {
// Schrodinger: file may or may not exist. See err for details.

// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence


}


Related Topics



Leave a reply



Submit