Given this example function:
def writeFile(listLine,fileName):
'''put a list of str-line into a file named fileName'''
with open(fileName,'a',encoding = 'utf-8') as f:
for line in listLine:
f.writelines(line+'\r\n')
return True
Does this return True statement do anything useful?
What's the difference between with it and without it? What would happen if there were no return function?
Trueor raise an error, so thereturnisn't too useful here. - Blenderf.writelines(line+'\r\n')happens to work, but it's incredibly wasteful; it's treating the argument as a sequence and writing out each element;strare iterables of their characters, so it's effectivelywrite-ing each character individually (buffering saves you from actual system call overhead, but it's still a lot more work).f.write(line+'\r\n')would get the same result, and write as a block, not character by character. - ShadowRanger