简体   繁体   中英

AttributeError: '_io.BufferedWriter' object has no attribute 'writer'

So my code is giving me the error:

AttributeError: '_io.BufferedWriter' object has no attribute 'writer'

any idea why? 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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM