【hacker的错误集】ValueError: I/O operation on closed file

简介: ValueError: I/O operation on closed file.依旧是使用单词意思来分析报错原因

78c5e154928c4d0bb6a0cd1fcc5664ff.jpg


报错内容



报错代码:


import csv
person = [('xxx', 18, 180), ('yyy', 18, 182), ('zzz', 19, 185)]
# 表头
header = ['name', 'age', 'height']
with open('person.csv', 'w', encoding='utf-8-sig') as file_obj:
    # 1:创建writer对象
    writer = csv.writer(file_obj)
    # 2:写表头
    writer.writerow(header)
    # 3:遍历列表,将每一行的数据写入csv
for p in person:
    writer.writerow(p)

7daf42dae7c244c69dfc55c3819e5f99.png


报错分析



ValueError: I/O operation on closed file.依旧是使用单词意思来分析报错原因

ValueError值错误

closed file关闭的文件


通过分析可以得出:with open处理了已经被关闭的数据。使用with open打开文件,如果语句在with open之外是无效的,因为文件已经被关闭了

居然:那应该怎么解决呢

hacker:👀👀👀


解决方案



其实解决方法很简单,只需要将你要处理的数据都加到with open里,改一下代码缩进即可完美解决


image.png


改进后的代码:


import csv
person = [('xxx', 18, 180), ('yyy', 18, 182), ('zzz', 19, 185)]
# 表头
header = ['name', 'age', 'height']
with open('person.csv', 'w', encoding='utf-8-sig') as file_obj:
    # 1:创建writer对象
    writer = csv.writer(file_obj)
    # 2:写表头
    writer.writerow(header)
    # 3:遍历列表,将每一行的数据写入csv
    for p in person:
        writer.writerow(p)


完美解决🥳🥳🥳

结束语🏆🏆🏆

会持续更新专栏《hacker的错误集》相关知识,如果有改进的建议欢迎在评论区留言奥~

感谢大家对hacker的支持💖💖💖


相关文章
|
4月前
|
iOS开发 Perl
解決pod install报错:unable to access: LibreSSL SSL_read: error:Operation timed out, errno 60
解決pod install报错:unable to access: LibreSSL SSL_read: error:Operation timed out, errno 60
85 0
openmv出现File corrupted,out of memery,invalid syntax等其他问题解决方法
openmv出现File corrupted,out of memery,invalid syntax等其他问题解决方法
187 0
|
数据采集 Python
|
SQL 算法 小程序
【hacker的错误集】IndentationError: expected an indented block
以猜数字的小程序为例做解答,遇到这种问题该如何解决
188 0
【hacker的错误集】IndentationError: expected an indented block
【hacker的错误集】DeprecationWarning: find_element_by_* commands are deprecated.
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead。依旧是使用单词意思分析报错原因
144 0
【hacker的错误集】DeprecationWarning: find_element_by_* commands are deprecated.
|
关系型数据库 MySQL Java
|
安全 iOS开发 MacOS
“XXXXX” is damaged and can’t be opened. You should move it to the Trash 解决方案
“XXXXX” is damaged and can’t be opened. You should move it to the Trash 解决方案
594 0
|
Go iOS开发
The operation couldn’t be completed. Unable to log in with account 'myappleid'. An unexpected failure occurred while logging in (Underlying error code 1100).解决方法
The operation couldn’t be completed. Unable to log in with account 'myappleid'. An unexpected failure occurred while logging in (Underlying error code 1100).解决方法
455 0
|
数据库
Symantec Backup Exec Agent 推送错误Error connecting to the remote computer. Ensure that the computer is available, has WMI enabled and is not blocked by a
如果在Symantec Backup Server上推送Symantec Backup Exec Agent到数据库服务器遇到“"Error connecting to the remote computer. Ensure that the computer is available, has WMI enabled and is not blocked by a firewall"这个错误, 如下截图所示     那么完全可以参考下面官方提供的三个解决方案解决问题,几次碰到这个问题,每次都要搜索一下,特此记录一下,方便以后查找。
1351 0
|
SQL 数据库
DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered
我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误:   Exec YourSQLDba.Maint.
1165 0