AttributeError:'_io.BufferedWriter'对象没有属性'writer'

问题描述 投票:0回答:2

所以我的代码给了我错误:

AttributeError:'_io.BufferedWriter'对象没有属性'writer'

知道为什么吗? 这是我的代码:

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")

谢谢你。

python attributeerror writer
2个回答
1
投票

File 对象 (

f
) 没有任何名为
writer
的方法。

请使用

f.write()


0
投票

你需要 2 变得更好,不再做坏事

© www.soinside.com 2019 - 2024. All rights reserved.