xxxxxxxxxx
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
/**
* \brief Return the filenames of all files that have the specified extension
* in the specified directory and all subdirectories.
*/
std::vector<fs::path> get_all(fs::path const & root, std::string const & ext)
{
std::vector<fs::path> paths;
if (fs::exists(root) && fs::is_directory(root))
{
for (auto const & entry : fs::recursive_directory_iterator(root))
{
if (fs::is_regular_file(entry) && entry.path().extension() == ext)
paths.emplace_back(entry.path().filename());
}
}
return paths;
}
xxxxxxxxxx
[ -f /etc/resolv.conf ] && { echo "$FILE exist."; cp "$FILE" /tmp/; }
xxxxxxxxxx
from pathlib import Path
if Path('filename.txt').is_file():
print ("File exist")
else:
print ("File not exist")
xxxxxxxxxx
# Brute force with a try-except block (Python 3+)
try:
with open('/path/to/file', 'r') as fh:
pass
except FileNotFoundError:
pass
# Leverage the OS package (possible race condition)
import os
exists = os.path.isfile('/path/to/file')
# Wrap the path in an object for enhanced functionality
from pathlib import Path
config = Path('/path/to/file')
if config.is_file():
pass