ValueError: I/O operation on closed file 的解决办法

运行爬虫文件一直出现如下错误:
Traceback (most recent call last):
File “20190418ceshiceshi.py”, line 60, in
f.write("{}".format(pic))
ValueError: I/O operation on closed file.

代码如下

with open('fenghuang.csv','a',encoding='utf-8') as f:
	for x in  etree_lists:
		#pic=x.xpath(r"./a/img/@alt")[0].strip()
		pic=x.xpath(r"./a/img/@alt")
		print(pic)
		print(''.join(pic))
		#print({"{}".format(pic)})
		#f.write(''.join(pic))
		#f.write(pic)
		f.write("{}".format(pic))
		#f.close()
	f.close()

后来发现是f.close() 缩进错误, 和for 下载同一级别即可解决此问题。

你可能感兴趣的:(ValueError: I/O operation on closed file 的解决办法)