Request for feedback on my first Python program
Bernard Delmée
bdelmee at advalvas.REMOVEME.be
Fri May 30 03:59:23 EDT 2003
> # if this language had a main(), it'd be here...
In python, the customary idiom would be:
if __name__ == '__main__':
# main script or call to function
This way your script works both standalone and as a module if needed.
> Aside from being ugly (how do I get rid of the newline that follows
> each directory or file name?), the problem is that the first entry IS
> a directory and the second one IS a file. So clearly I'm doing
> something wrong. Any idea what it is?
As shown in your output, the newlines are what cause the "ugly" *and*
wrong result. Every line returned by readlines() ends with a NL which
you need to strip before accessing/testing the file it represents.
So insted of
> for i in lines:
> # ...
You could say
for l in lines:
i = l.strip()
# ...
Now, when can we expect "effective python" ?-)
But then python does not nearly have the pitfalls and idiosyncrasies
of C++, which one can only truly comprehend after reading your very
fine books. Thanks for these.
Regards,
Bernard.
More information about the Python-list
mailing list