类型错误需要字符串或缓冲区,在python中找到的文件

Cha*_*d D 0 python

因此,当我单独编写这一块代码时,它工作正常但是当我将它们组合在一起时它会给我typeError.为什么会这样?当我单独写它时它我没有得到它它工作正常.提前致谢 :)

def printOutput(start, end, makeList):

  if start == end == None:

      return

  else:

      print start, end

      with open('OUT'+ID+'.txt','w') as outputFile:#file for result output
          for inRange in makeList[(start-1):(end-1)]:
              outputFile.write(inRange)
          with open(outputFile) as file:
              text = outputFile.read()
      with open('F'+ID+'.txt', 'w') as file:
        file.write(textwrap.fill(text, width=6))
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 5

你的问题出在这一行:

 with open(outputFile) as file:
Run Code Online (Sandbox Code Playgroud)

outputFile是一个文件对象(已经打开).该open函数需要一个字符串(或类似的东西),它是要打开的文件的名称.

如果你想背课文,你可以随时outputFile.seek(0)outputFile.read()一次.(当然,你必须以r+模式打开才能工作.)

或许更好的方法是:

with open('OUT'+ID+'.txt','w') as outputFile:#file for result output
    text=''.join(makeList[(start-1):(end-1)])
    outputFile.write(text)
with open('F'+ID+'.txt', 'w') as ff:
    ff.write(textwrap.fill(text, width=6)) #Version of above file with text wrapped to 6 chars.
Run Code Online (Sandbox Code Playgroud)

编辑

这应该工作:

def printOutput(start, end, makeList):
    if start == end == None:
        return
    else:
        print start, end

        with open('OUT'+ID+'.txt','w') as outputFile:#file for result output
            text=''.join(makeList[(start-1):(end-1)])
            outputFile.write(text)
        with open('F'+ID+'.txt', 'w') as ff:
            ff.write(textwrap.fill(text, width=6)) #Version of above file with text wrapped to 6 chars.
Run Code Online (Sandbox Code Playgroud)