ZoWebdev

Web ontwikkeling voor Jongeren
[ 19-01-2010 | 18:15 ]

Essentials

import sys, string, os
os.mkdir()
len(string)

 


write to file

f = open(base+'test.txt', 'w')
f.write()
f.close()

 


read file line by line into array

ins = open( "file.txt", "r" ) array = [] for line in ins: array.append( line )

 


define a function

def printme( str ):
"This prints a passed string into this function"
print str
return

call an imported function

You may define a function in a separate python file and import it as the sys and os imports.
you can then call the function as follows:

import pfile #if your function is stored in pfile.py
pfile.fn() #if your funtion is called fn()

is my string a number?

str.isdigit()

loops

print range(5)
for x in range(3,12,2):
     print x

l = ['test','dat','pos']
for x in range(len(l)):
     print x, l[x]

for x in ['a', 'b', 'c']:
     print x

x=0
while x<12:
     print x
     x=x+1

do files or dirs exist? else make them

    os.path.exists(filename)
    os.path.isdir(dirname)
    os.mkdir(dirname)