Skip to content

Latest commit

 

History

History
executable file
·
75 lines (52 loc) · 942 Bytes

File metadata and controls

executable file
·
75 lines (52 loc) · 942 Bytes

files

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()

Reference and read more

Reading and Writing Files

Revision history

2014-07-21 (sylvanas) PA1 First try.