Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我不断地遇到这个错误,我不知道该怎么解决它_Python - Fatal编程技术网

Python 我不断地遇到这个错误,我不知道该怎么解决它

Python 我不断地遇到这个错误,我不知道该怎么解决它,python,Python,我打开了文件,但是说文件没有打开。我坚持要做什么。我是python新手 以下是错误: Traceback (most recent call last): File "\\users2\121721$\Computer Science\Progamming\task3.py", line 43, in <module> file.write(barcode + ":" + item + ":" + price + ":" +str(int((StockLevel- floa

我打开了文件,但是说文件没有打开。我坚持要做什么。我是python新手

以下是错误:

Traceback (most recent call last):
File "\\users2\121721$\Computer Science\Progamming\task3.py", line 43, in   <module>
file.write(barcode + ":" + item + ":" + price + ":" +str(int((StockLevel-    float(HowMany)))))
    ValueError: I/O operation on closed file.

if barcode!=UsersItem:
    open("data.txt","w")
你需要

if barcode!=UsersItem:
    file = open("data.txt","w")
还有与
else
语句中相同的错误

你也应该考虑重构你的代码,因为你有很多文件打开和关闭。 编辑:正如@roganjosh所提到的,

file
是Python 2中的一个内置名称,因此您最好将所有出现的
file
更改为,例如,
f

if barcode!=UsersItem:
    open("data.txt","w")
你需要

if barcode!=UsersItem:
    file = open("data.txt","w")
还有与
else
语句中相同的错误

你也应该考虑重构你的代码,因为你有很多文件打开和关闭。


编辑:正如@roganjosh所提到的,
file
是Python 2中的一个内置名称,因此您最好将所有出现的
file
更改为,例如
f

您没有创建
file
对象。如果没有
file=open(文件名,模式),如何执行
file.write
anywhere near.你没有创建
文件
对象如何创建
文件.写
,如果附近没有
文件=打开(文件名,模式)
任何地方.我不建议建议他们使用
文件
内置名…@roganjosh
('file'在dir(\uu builtin\uuuuuuu)中)是假的
Hmm,有趣。这是一个内置的,但不是。@roganjosh你是对的,我已经更新了答案。谢谢,伙计!不用担心,我当时有点困惑,因为我的编辑器总是为
文件更改颜色:)我想我从来没有用过它,所以它被删除也就不足为奇了。我不建议建议他们使用
文件
内置名…@roganjosh
('file'in dir(uu builtin_uu))是假的
嗯,有趣。这是一个内置的,但不是。@roganjosh你是对的,我已经更新了答案。谢谢,伙计!不用担心,我当时有点困惑,因为我的编辑器总是更改
文件的颜色(我不认为我曾经使用过它,尽管tbh,所以它被删除并不完全令人惊讶