Python 2.4.4不间断类型错误

h4u*_*t3r 0 python typeerror

我使用python 2.4.4并且由于某种原因它在我尝试打开日志文件进行写入时不断抛出类型错误...这是有问题的函数...

import os
def write(inlog, outlog):
  # parse the logs and save them to our own...
  parsed = NTttcpParse(inlog)
  debug("PARSED")
  debug(parsed)
  if os.path.exists(outlog):
    fh = os.open(outlog, os.O_WRONLY | os.O_APPEND)
    debug("Opened '%s' for writing (%d)" % (outlog, fh))
  else:
    fh = os.open(outlog, os.O_WRONLY | os.O_CREAT)
    debug("Created '%s' for writing (%d)" % (outlog, fh))
  debug("type(fh) = %s" % type(fh))
  os.write(fh, LOGFORMAT % parsed)
  os.close(fh)
Run Code Online (Sandbox Code Playgroud)

这是令人抓狂的错误......

TypeError: int argument required
Run Code Online (Sandbox Code Playgroud)

请帮助......并提前感谢:P

Bre*_*arn 5

你正在以一种奇怪的方式进行文件I/O. 这是做到这一点的方法:

f = open(outlog, "w")
f.write("some data written to file\n")
f.close()
Run Code Online (Sandbox Code Playgroud)

如果要追加,请open(outlog, "a")改用.如果您想阅读,请使用open(outlog, "r").另请阅读Python教程,该教程解释了这样的基本文件I/O操作.

请注意,在Python 2.5及更高版本中,您可以使用以下with语句:

with open(outlog, "w") as f:
    f.write("some data written to file\n")
Run Code Online (Sandbox Code Playgroud)

(在我注意到你说你使用的是2.4之前,我最初发布这个作为主要答案.)