[Tutor] Parsing /etc/passwd
Peter Otten
__peter__ at web.de
Wed Oct 12 16:27:13 CEST 2011
Hugo Arts wrote:
>> f = open('/etc/passwd', 'r')
> users = f.readlines()
> for user in users:
...
You can iterate over the file directly:
for user in f:
...
The version using readlines() reads the whole file into a list of lines
where the alternative just has to remember the current line.
While it doesn't matter much for small files iterating over the file
directly will save a lot of memory if the input file is large.
Therefore avoiding readlines() is a good habit to get into.
More information about the Tutor
mailing list