change the first character of the line to uppercase in a text file

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Jun 28 01:36:06 EDT 2009


On Sat, 27 Jun 2009 19:55:17 +0200, Piet van Oostrum wrote:

>>>>>> Terry Reedy <tjreedy at udel.edu> (TR) wrote:
> 
>>TR> Peter Otten wrote:
>>>>>> Will your program handle empty lines of input correctly?
>>>>> Strangely enough, it seems to do so, but why?
>>>> 
>>>> Because there aren't any. When you read lines from a file there will
>>>> always be at least the newline character. Otherwise it would indeed
>>>> fail:
> 
>>TR> Except possibly for the last line.
> 
> But then that line wouldn't be empty either.
> 
> If there is an empty line not terminated by a newline after the last
> newline, then that is called 'end-of-file' :=)


I try to always write file-handling files under the assumption that some 
day somebody (possibly me) will pass the function a file-like object, and 
therefore make the minimum number of assumptions about each line.

For example, I wouldn't assume that lines can't be empty, or that they 
must end in a newline. The later is violated even by ordinary files, but 
the former could be violated by a file-like object which iterated over 
(say) ['first line', 'second line', '', 'the previous line was blank'].



-- 
Steven



More information about the Python-list mailing list