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
170
Python
Datum, Zeit
Gestriges Datum
from datetime import date, timedelta
d=date.today()-timedelta(days=1)
output=d.isoformat()
# 2014-02-27
186
Python
Strings
String nach Delimenter zerlegen
myString = "A;B;C;D;E"
myPieces = myString.split(";") # erzeugt ein Arrey mit den einzelnen Zeichen
print myPieces[2] # gibt C aus
188
Python
Prozeduren, Funktionen, Methoden
Deklaration einer Funktion/Prozedur
def this_is_my_function_name(param1):
print "do something inside the function " + param1
print "do something outside the function"
# call the function
this_is_my_function_name("hello world")
189
Python
Schleifen
For-Schleife
for i in range(0, 10):
i=1
time.sleep(1)
print i
items = ['computer', 'fruits', 'fraggels']
# without indexes
for item in items:
print item
# with indexes
for (i, item) in enumerate(items):
print i, item
190
Python
Schleifen
While Schleife
i=10
while i<10:
i=i+1
print i
191
Python
Encoding / Decoding
md5-Hash erzeugen
# https://docs.python.org/2/library/hashlib.html
import hashlib
a = "hello world"
b = 0.1
md5str = hashlib.md5(str(b) + a).hexdigest()
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 :-("
193
Python
Array
Dictionary Basics
# empty dict
myDict = {}
# some static values
myDict ={
"key1" : {"item1":"value1"},
"key2" : {"itemX":"valueY"},
"keyX" : {"item99":"value98"}
}
# update a key programmatically
myDict.update({"key1" = {"item2":"new value"}})
# iterate
for (key, subdict) in myDict.items():
current_key = key
current_subdict = subdict
# get an specific item
subdict = myDict.get("key2")
print subdict.get("itemX")
# valueY
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