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))
Your problem is at this line:
outputFileis a file object (which is already open). Theopenfunction 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 thenoutputFile.read()again. (Of course, you’ll have to open inr+mode for this to work.)Perhaps an even better way to do this would be:
EDIT
This should work: