Collin Funk wrote: > With open() using binary mode with encoding='utf-8' causes a failure: > > with open('test.txt', 'wb', encoding='utf-8') as file: > file.write('abc') > > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > ValueError: binary mode doesn't take an encoding argument
Oops, you're right. My mistake. And likewise for 'rb'. > # Write files with '\n' as newline character. > with open('file.txt', 'w', encoding='utf-8', newline='\n') as file: Yes, you're right. I've committed a fix now. Sorry. > From the documentation from open, it seems the best way to deal with > this is for reading files [2]: > > # Accepts '\n', '\r', '\r\n' as newline. > with open('file.txt', 'r', encoding='utf-8') as file: > data = file.read() They can recommend it. But what we want here is to recognize Unix newlines, not macOS 9 newlines or DOS/Windows newlines. It should behave like gnulib-tool.sh, and thus newline='\n' is appropriate here. Bruno