Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • ok, sorry, you are absolutely right. but maybe this solution will make you happier ;) : s = "" for i in xrange(100): s += file.next() Commented Feb 6, 2009 at 10:58
  • 3
    -1: Terrible solution, this would mean creating a new string in memory each line, and copying the entire file data read to the new string. The worst performance and memory. Commented Feb 6, 2009 at 15:28
  • why would it copy the entire file data into a new string? from the python documentation: In order to make a for loop the most efficient way of looping over the lines of a file (a very common operation), the next() method uses a hidden read-ahead buffer. Commented Feb 6, 2009 at 15:37
  • 3
    @sinzi: "s +=" or concatenating strings makes a new copy of the string each time, since the string is immutable, so you are creating a new string. Commented Feb 6, 2009 at 16:50
  • 1
    @nosklo: these are details of implementation, list comprehension can be used in it's place Commented Feb 6, 2009 at 17:05