
![]() | Open and read:
f = open(filename, 'r')
filestr = f.read() # reads the whole file into a string
lines = f.readlines() # reads the whole file into a list of lines
for line in f: # read line by line
<process line>
while True: # old style, more flexible reading
line = f.readline()
if not line: break
<process line>
f.close()
|
![]() | Open and write:
f = open(filename, 'w') f.write(somestring) f.writelines(list_of_lines) print >> f, somestring |