View Category

Read the contents of a file into a string

python
contents = open('myFile.txt', 'rt').read()

Process a file one line at a time

Open the source file to your solution and print each line in the file, prefixed by the line number, like:
1> First line of file
2> Second line of file
3> Third line of file
python
for no, line in enumerate(open(__file__)):
print "{0}> {1}".format(no+1, line.rstrip())

Write a string to a file

python
open('test.txt', 'wt').write('Hello World!')

Append to a file

python
open('test.txt', 'at').write('Hello World!\n')

Process each file in a directory

python
import os
results = (process(f) for f in os.listdir(".") if os.path.isfile(f))

Process each file in a directory recursively

python
import os
results = (process(os.path.join(p, n)) for p,d,l in os.walk(".") for n in l)