Examples in Python 3
"""
Open file
"""
fhand = open("test.txt")
print(fhand)
"""
Read whole file
"""
print(fhand.read())
"""
Return to start of file
"""
fhand.seek(0)
"""
Read lines
"""
print(fhand.readline())
print(fhand.readline())
"""
Read all lines
"""
fhand.seek(0)
for line in fhand:
print(line, end="")
print()
"""
Close file
"""
fhand.close()
"""
Write file
"""
# Open the file with appending mode
fhand = open("test.txt", "a")
inp = input("Enter what you want to add to the file: ")
fhand.write("\n" + inp)
fhand.close()
# Open the file for reading
fhand = open("test.txt")
print(fhand.read())
fhand.close()
2014-07-21 (sylvanas) PA1 First try.