为什么Python给我“需要整数"?什么时候不应该? [英] Why is Python giving me "an integer is required" when it shouldn't be?
本文介绍了为什么Python给我“需要整数"?什么时候不应该?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Python程序中有一个保存函数,如下所示:
I have a save function within my Python program which looks like this:
def Save(n):
print("S3")
global BF
global WF
global PBList
global PWList
print(n)
File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w")
pickle.dump(BF, File)
File = open("C:\KingsCapture\Saves\\" + n + "\WF.txt", "w")
pickle.dump(WF, File)
File = open("C:\KingsCapture\Saves\\" + n + "\PBList.txt", "w")
pickle.dump(PBList, File)
File = open("C:\KingsCapture\Saves\\" + n + "\PWList.txt", "w")
pickle.dump(PWList, File)
在这里,n为"1".
我看到这样的错误:
File "C:/Python27/KingsCapture.py", line 519, in Save
File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w")
TypeError: an integer is required
在外壳中执行相同的加载后,我没有出现任何错误:
Upon doing the same loading within the shell, I get no errors:
>>> File = open("C:\KingsCapture\Test\List.txt", "r")
>>> File = open("C:\KingsCapture\Test\List.txt", "w")
>>> n = "1"
>>> File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "r")
>>> File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w")
为什么会有问题?
推荐答案
您可能从os模块中导入了星号:
You probably did a star import from the os module:
>>> open("test.dat","w")
<open file 'test.dat', mode 'w' at 0x1004b20c0>
>>> from os import *
>>> open("test.dat","w")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
因此您使用了错误的打开功能. (我想您可以简单地完成from os import open
,但这不太可能.)通常应避免这种导入样式,在可行的情况下也应避免使用global
.
so you're using the wrong open function. (I suppose you could've simply done from os import open
, but that's less likely.) In general this import style should be avoided, as should use of global
, where practical.
这篇关于为什么Python给我“需要整数"?什么时候不应该?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文