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.
import commands
output=commands.getoutput("dir")
print output
# home tmp myFile.sh
175
Bash Script
Filehandling
Verzeichnis rekursiv durchsuchen
# http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#Variablen-Teil-2
#!/bin/bash
for file in /home/myUser/*/*.png ; do
echo "Diese Datei: $file"
fname=$(basename "$file")
echo "hat den Namen: $fname"
fdir=$(dirname "$file")
echo "und steht im Verzeichnis: $fdir"
# Diese Datei: /home/myUser/galaxy.png
# hat den Namen: galaxy.png
# und steht im Verzeichnis: /home/myUser
176
Bash Script
Filehandling
Prüfen ob Datei existiert
# http://wiki.ubuntuusers.de/Shell/Bash-Skripting-Guide_f%C3%BCr_Anf%C3%A4nger#If-Else-Anweisung
#!/bin/bash
filePath="/home/myUser/test.txt"
if [ -f ${filePath} ]
then
echo "There it is! ;-)"
else
echo "Whow, file is missing at this place :-("
fi
177
Delphi
Filehandling
Prüfen ob Datei existiert
procedure TForm1.CheckIfFileExists();
var
filePath : String;
begin
filePath:='C:\User\myUser\text.txt';
if FileExists(filePath) then
begin
ShowMessage('There it is! ;-)');
end
else
begin
ShowMessage('Whow, file is missing at this place :-(');
end;
end;
178
C#
Filehandling
Prüfen ob Datei existiert
String filePath = "C:\\User\\myUser\\text.txt";
if (System.IO.File.Exists(filePath))
{
MessageBox.Show("There it is! ;-)");
}
else
{
MessageBox.Show("Whow, file is missing at this place :-(");
}
179
PHP
Filehandling
Prüfen ob Datei existiert
$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;
}