open(): opens a file stream with open() as streamName:
#!/usr/bin/env python3
import os # miscellaneous operating system interfaces
myText = "However fast you may be able to think,\nit is not fast enough."
f_out = open("output.txt", "w") # output to file
f_out.write(myText)
f_out.close() # closing stream
os.system("cp output.txt input.txt") # copy files
f_in = open("input.txt") # reading is the default
print("# f_in :\n", f_in.read()) # file to string --> print
f_in.close()
print()
with open("input.txt", "r") as g_in: # open with 'with'
lineCount = 0
for line in g_in: # reading line by line
lineCount = lineCount + 1
print(f'reading/printing line {lineCount:3d} ',line)
# cleaning up
if 1==2:
os.remove("input.txt")
os.remove("output.txt")
replace(): string to string
split(): string to list
#!/usr/bin/env python3
# line continuations with ( ... )
myText = ( "However fast you may be able to think,\n"
+ "it is not fast enough.\n"
+ "Don't worry, others have the same problem;)" )
# direct use of file streams
open("thinking.txt", "w").write(myText)
data = open("thinking.txt", "r").read()
# line continuations with \
for token in data.replace('\n',' ').replace('.',' ').\
replace(', ',' ').replace(' ',' ').split(" "):
print(token)
yield data returning multiple times
return data returning a single time
range() is a generator (start, stop, step)
myString[i:j] extracts chars from i to j-1
myString[i:] form i to the end
#!/usr/bin/env python3
def tokenizer(inString):
sepChar = " "
lastPos = 0
inString = inString + sepChar # seperation token
for ii in range(len(inString)):
if inString[ii] == sepChar:
yield inString[lastPos:ii]
# print(ii, inString, inString[lastPos:ii])
lastPos = ii + 1
def fibonacci(maxN):
x, y = 0, 1
for _ in range(maxN): # no variable needed
x, y = y, x+y
yield x
myText = "Warum bin ich hier?"
myList = [x for x in myText]
print("\nmyText\n",myText)
print("\nmyList\n",myList)
print()
myGenerator = tokenizer(myText)
print("type(myText) : ", type(myText))
print("type(myList) : ", type(myList))
print("type(myGenerator) : ", type(myGenerator))
print()
print("*** myList ***")
for cc in myList:
print(cc, end='') # no endline
print()
print()
print("*** myGenerator ***")
for cc in myGenerator:
print(cc, "", end='')
print()
print("*********")
print("fibonacci")
print("*********")
[print(ff) for ff in fibonacci(5)] # generator comprehension
print()
print(sum(fibonacci(5))) # shortcut
#!/usr/bin/env python3
import math
def myLog(x):
if not type(x) is float:
raise TypeError("only myLog(x) if x is float")
if x <= 0:
raise Exception("only myLog(x) when x>0")
return math.log(x)
print("************************")
print("basic exception handling")
print("************************")
try:
print(x)
except NameError:
print("undefined variable")
except:
print("something went wrong")
print()
print("*******************************************")
print("catching user-defined exceptions / messages")
print("*******************************************")
try:
myLog(0.0)
except TypeError as message:
print("a type error occured: " + str(message))
except Exception as message:
print("something went wrong: " + str(message))
else:
print("nothing went wrong")
finally: # always executed
print("'try except finally' finished")
print()
print("***********************")
print("input/output exceptions")
print("***********************")
fileName = "demo.txt"
try:
f = open(fileName) # default, reading only
try:
f.write("Lorum Ipsum")
except:
print("failed to write to " + fileName)
finally:
f.close()
except:
print("failed opening of " + fileName)
sys.argv: list of arguments when running ./code.py par1 par2 ...
sys.argv[0]: (self) executable (as in C++)
with ... as ...:
compressed try-catch block
#!/usr/bin/env python3
import sys # standard system modul
import os # miscellaneous operating system interfaces
fN = 'counting.txt'
if not os.path.isfile(fN): # 'is file'
with open(fN, 'w') as f: # short for try-catch block
f.write("1")
with open(fN, 'r+') as g: # read and write
if (var:=int(g.read())) >= 100000: # exit when too large
exit()
print("content of ", fN, " : ", var)
g.write("0") # appending, because did read first
print(sys.argv)
os.system(sys.argv[0]) # running self
#!/usr/bin/env python3
import pickle
# any Python object:
# dictionary, list, ..
# instance of a class, tensor, ..
# lists of classes, tensors, ..
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# binary serialization, dumping to file
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
# deserialization, loading from file
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
print(loaded_data)