Why is Python giving me “an integer is required” when it shouldn't be?

巧了我就是萌 提交于 2019-11-29 10:35:15

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

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.

You need to escape your strings: a \ in a string is an escape character.

Either escape the slashes:

"C:\\KingsCapture\\Test\\List.txt"

or use Raw strings:

r"C:\KingsCapture\Test\List.txt"

As DSM noted, you're using http://docs.python.org/library/os.html#os.open instead of built-in open() function.

In os.open() the second parameter (mode) should be integer instead of string. So, if you ought to use from os import * then just substitute mode string with one of the following args:

  • os.O_RDONLY
  • os.O_WRONLY
  • os.O_RDWR
  • os.O_APPEND
  • os.O_CREAT
  • os.O_EXCL
  • os.O_TRUNC

I'll bet that n is 1 not "1".

try:

print(type(n))

I'll guess that you'll see its an int not a string.

File = open("C:\KingsCapture\Saves\\" + n + "\BF.txt", "w")

You can't add ints and strings producing the error message you are getting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!