files are a set of bytes used to store data
bytes are translated into binary
Opening and Closing a file
file = open('file_name.txt')
reader = open('file_name.txt')
try:
finally:
reader:close
with open('file_name.txt') as reader
with python('file_name.txt', 'r') as reader:
.read(size=-1)
.readline(size=-1)
.readlines()
>>> with open('dog_breeds.txt', 'r') as reader:
>>> # Read and print the entire file line by line
>>> line = reader.readline()
>>> while line != '': # The EOF char is an empty string
>>> print(line, end='')
>>> line = reader.readline(
.write(string)
.writelines(seq)
with open('dog_breeds.txt', 'r') as reader:
# Note: readlines doesn't trim the line endings
dog_breeds = reader.readlines()
with open('dog_breeds_reversed.txt', 'w') as writer:
# Alternatively you could use
# writer.writelines(reversed(dog_breeds))
# Write the dog breeds to the file in reversed order
for breed in reversed(dog_breeds):
writer.write(breed)