Why Can't I use a file object more than once when using: with open(filename) as newname syntax?

link: https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/reading-different-csvs
When I try to run this code, only the first for loop gets ran in both cases and I’m wondering why?

import csv
with open('books.csv', newline='') as books_csv:
  books_reader = csv.DictReader(books_csv, delimiter='@')
  isbn_list = [book['ISBN'] for book in books_reader]
  for row in books_reader:
    print(row['Author'])

print(isbn_list)

with open('addresses.csv', newline='') as address:
  address_dict = csv.DictReader(address, delimiter = ';')
  for row in address_dict:
    print(row['Name'])
  for line in address.readlines():
    print(line)

Hello @jsmoodii ,

Here’s a good explanation.

2 Likes