My code is giving me this error:
AttributeError: '_io.BufferedWriter' object has no attribute 'writer'
Here is my code:
import requests
import time
import sys
start = time.perf_counter()
flags = open('flags.txt', 'r')
countryCode = []
totalBytes = 0
for line in flags.readlines():
countryCode.append(line.strip())
for country in countryCode:
url = f"https://www.cia.gov/library/pulications/resources/the-world-factbook/graphics/flags/large/{country}-lgflag.gif"
r = requests.get(url)
with open(f"{country}Flag.gif", 'wb') as f:
f.writer(r.content)
totalBytes+= sys.getsizeof(f)
end = time.perf_counter()
print ("elapsed time:", end - start)
print (totalBytes, "bytes dowloaded")
Thank you.
The File object (f
) does not have any method called writer
.
Instead, use f.write()
.