[Python-Dev] Re: Optional separatorargument for file.writelines() and StringIO.writelines()

Vernon Cole kf7xm at netscape.net
Thu Feb 26 13:31:44 EST 2004


This is a big YES!
   A function or method should do what is says. At present, writelines() 
should be named append(), because it does not actually write LINES.   Us 
older folks often forget which environment we are working in. I am too 
used to typing "write" when I do not want a line terminator, and 
"writeln" when I do. (This agrees well with Python where, if I do NOT 
want an automatic end-of-line, I must put a trailing comma in the Print 
command.) But, suddenly, when I am sending text to a file, I must 
remember to hang "\n" on the end of everything. Confusing and 
non-user-friendly.
   Backslash-N is not exactly a worldwide way of stating the concept "go 
to a new line."  Not everyone is a C programmer.
   This also ties in with the "UNIVERSAL_NEWLINE" thing. Only in *nix 
does a linefeed charactor equate to the end of a line in a text file. 
So, in every output line of every program I must pretend to be a C 
programmer and append a '\n' so that the runtime system can replace it 
with the actual End-of-Line used by my operating system. N.U.T.S.! Let 
me tell the file method that I want a UNIVERSAL_NEWLINE added to each 
line I send and let it be done in one step.

    Let it be:
      file.writelines(x for x in mylines, True)

    or even better:
      file.writeNewlines(True)
      file.writelines(x for x in mylines)

    Then in Python 3.0 file.writeNewlines can be defaulted True for all 
files opened in TEXT mode.  Perhaps that will help the *nix crowd 
remember to actually OPEN binary files in binary mode.
------------
Vernon
..............
Dmitry Vasiliev wrote:

> Raymond Hettinger wrote:
> 
>> Currently, writelines() does not add trailing line separators..
>> This is fine when working with readlines() but a PITA in other 
>> situations.
>>
>> If we added an optional separator argument, it would be easier to add
>> newlines and we would gain some of the flexibility of str.join() at 
>> full C speed.
> 
> 
> Maybe not a separator but suffix, so newline will be added to last line 
> too?
> 





More information about the Python-Dev mailing list