Answered by : dead-deer-2osfixfyazpe
>>> import os
>>> os.path.isfile("d:\\Package1\\package1\\fibo.py")
True
>>> os.path.isfile("d:/Package1/package1/fibo.py")
True
>>> os.path.isfile("d:\\nonexisting.txt")
Source : https://www.tutorialspoint.com/How-do-I-check-whether-a-file-exists-using-Python | Last Update : Tue, 01 Sep 20
Answered by : redouan
[ -f /etc/resolv.conf ] && { echo "$FILE exist."; cp "$FILE" /tmp/; }
Source : https://linuxize.com/post/bash-check-if-file-exists/ | Last Update : Wed, 09 Mar 22
Answered by : impossible-impala-2kf2sz6ngusb
# check if file exists
from os.path import exists
file_exists = exists("/content/sample_data/california_housing_test.csv")
print(file_exists)
#True
from pathlib import Path
path = Path("/content/sample_data/california_housing_test.csv")
path.is_file()
#False
Source : | Last Update : Sat, 02 Apr 22
Answered by : david-k
# 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
Source : https://dev.to/renegadecoder94/71-python-code-snippets-for-everyday-problems-1mep | Last Update : Fri, 07 Oct 22
Answered by : ashwin-rajendran
#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;
}
Source : https://stackoverflow.com/questions/11140483/how-to-get-list-of-files-with-a-specific-extension-in-a-given-folder | Last Update : Fri, 17 Jun 22
Answered by : keenan-du-plessis
<SERVER>
// Open a log file
var myLog = new File("/data/logs/today.log");
// See if the file exists
if(myLog.exists()){ write('The file exists');
}else{ write('The file does not exist');
}
</SERVER>
Source : https://www.oreilly.com/library/view/pure-javascript/0672315475/0672315475_ch08lev2sec75.html | Last Update : Wed, 30 Nov 22