0

So when i write this chunk of code separately, it works fine but when i combine them together it give me typeError. Why is this happen? I don't get it when i wrote them separately it works fine. thanks in advance :)

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))
0

1 Answer 1

5

Your problem is at this line:

 with open(outputFile) as file:

outputFile is a file object (which is already open). The open function wants a string (or something like it) which is the name of a file to open.

If you want to get the text back, you can always outputFile.seek(0) and then outputFile.read() again. (Of course, you'll have to open in r+ mode for this to work.)

Perhaps an even better way to do this would be:

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.

EDIT

This should work:

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.
Sign up to request clarification or add additional context in comments.

8 Comments

what is the number inside seek() means and are you saying that i should use with open(outputFile, 'r+) as dile and outputFile.seek(0) and outputFile.read()
The number inside seek (0) is the file position. In this case, 0 is the beginning of the file. And yes, I am saying use open(outputFile, 'r+), but rather than seeking, see my updated answer which gets the text all together and then writes it all at once, keeping a handle on the text to use with textwrap
I guess my goal is to write the file and format it so that only 6 character per line
@ChadD -- You have two files. The first file 'OUT'+ID+'.txt' and the second file 'F'+ID+'.txt'. The second file will have the text wrapped at 6 characters. The first file won't.
i will update the code that i have.. i m not quite clear of what i should do but what i have above still doesnt work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.