Extra Topic: Basic File IO
Note: extra topics are not in CS Academy, but are required
and can appear on any future
homework, writing session, quiz, or exam.
# Note: As this requires read-write access to your hard drive,
# this will not run in the browser in Brython.
def readFile(path):
with open(path, 'rt') as f:
return f.read()
def writeFile(path, contents):
with open(path, 'wt') as f:
f.write(contents)
contentsToWrite = 'This is a test!\nIt is only a test!'
writeFile('foo.txt', contentsToWrite)
contentsRead = readFile('foo.txt')
assert(contentsRead == contentsToWrite)
print('Open the file foo.txt and verify its contents.')