Reading and Writing Files
# Open a file and assign to variable
f = open('file_name.txt')
# Open same file and specify read text mode
f = open('file_name.txt', 'rt')
# Read and print file
print(f.read())
# Write to a file
f = open("test.txt", "a")
f.write("test line")
f.close()
# Find more information
print(f.name)
print(f.closed)
print(f.mode)
# Use file object as iterator
with open('rockyou.txt, encoding='latin-1') as f:
for line in f:
pass
Last updated