Breaking the barrier of a broken paradigm... part 1

Ryan Ginstrom software at ginstrom.com
Tue Mar 25 17:41:03 EDT 2008


> On Behalf Of Bruno Desthuilliers
> >> for line in open("/etc/passwd"):
> 
> NB : this idiom relies on the VM automatically closing files, 
> which is not garanteed on each and every implementation 
> (IIRC, jython won't do it). This is ok for Q&D throwaway 
> scripts targeting CPython, but should not go into production code.

You're right. For production, I'd probably do this.

def create_user_dirs(lines):
    for line in lines:
        pass # process lines here

with open("/etc/passwd") as fp:
    create_user_dirs(fp)

This has the benefit of allowing me to test create_user_dirs without
touching the file system (by passing in a list of lines).

Regards,
Ryan Ginstrom




More information about the Python-list mailing list