Question : check if a file exists
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
Question : Check File Exists
Answered by : redouan-rida
[ -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
Question : # check if file exists
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
Question : Check if a file exists in the file system or not?
Answered by : pawan-prasoon-mishra
File file = new File(fileName);
boolean fileExist = file.exists();
boolean dirExist = file.isDirectory();
if(fileExist && !dirExist) System.out.println("File Exists and it's not a directory!");
Source : | Last Update : Wed, 03 Aug 22
Question : Checking if a File Exists
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