You may optionally enter a comparison operator (<, <=, >, >=, <> or =) at the beginning of each of your search values to specify how the comparison should be done.
$filePath = "/home/myUser/test.txt";
if (file_exists($filePath))
{
echo "There it is! ;-)"
}
else
{
"Whow, file is missing at this place :-("
}
184
Batch
Filehandling
Prüfen ob Datei existiert
@echo off
SET checkFileExists="C:\Temp\myFile.txt"
IF exist %checkFileExists% GOTO :EXISTS
GOTO :EXISTSNOT
:EXISTS
echo There it is :-)
GOTO :END
:EXISTSNOT
echo Sorry, the file isn't there :-(
GOTO :END
:END
echo End of program
192
Python
Filehandling
Prüfen ob Datei existiert
import os
filePath="/home/myUser/test.txt"
if (os.path.isfile(filePath)):
print "There it is! ;-)"
else:
print "Whow, file is missing at this place :-("
194
Python
Filehandling
Filename-Manipultation Basics
import os
myFilename = "/tmp/Test/myFile.txt"
# Get only the path
path_only = os.path.dirname(myFilename)
# /tmp/Test
# Filename only
filename_only = os.path.basename(myFilename)
# myFile.txt
# get only filename without extention
filename_without_ext = os.path.basename(os.path.splitext(myFilename)[0])
# myFile
# get only the extension
filename_extention_only = os.path.splitext(myFilename)[1]
# .txt
# put them together again
myFilename_together = os.path.join(path_only, filename_without_ext + filename_extention_only)
# /tmp/Test/myFile.txt
203
Java
Filehandling
Ternary operator
String thisVar = "OK";
boolean resultVar;
resultVar = thisVar.equals("OK") ? true : false;
// same as if-else construction
if (thisVar.equals("OK"))
{
resultVar = true;
}
else
{
resultVar = false;
}
246
Bash Script
Filehandling
Filename-Manipultation Basics
#!/bin/sh
myFilename="$1"
# or
myFilename="/tmp/Test/myFile.txt"
# Get only the path
path_only=$(dirname "$myFilename")
# /tmp/Test
# if it is a local file in the path, then . will be prompted e.g. myFilename="myFile.txt"
# Filename only
filename_only=$(basename "$myFilename")
# myFile.txt
# get only filename without extention
filename_without_ext="${filename_only%.*}"
# myFile
# get only the extension
filename_extention_only="${myFilename##*.}"
# txt
# put them together again
myFilename_together="${path_only}/${filename_without_ext}.${filename_extention_only}"
# /tmp/Test/myFile.txt