[Tutor] optional parameter in list comprehension

bob gailer bgailer at gmail.com
Mon Dec 29 16:57:03 CET 2008


prasad rao wrote:
> hello!
>     I am a novice looking up to the tutor as my Guide.
>     I got a problem   while using optional parameter.
>
> #! user/bin/env python
> import os
>
> def myfiles(directory,extension=None):

change that to def myfiles(directory,extension=""):

>     for x in [os.path.join(x,i) for x,y,z in os.walk(directory)\
>     for i in z if i.endswith(extension)]:print x
>     
> if __name__=='__main__':
>   import sys
>   myfiles(sys.argv[1],sys.argv[2])
>
> >>> myfiles.myfiles('C:\\python26')
>
> Traceback (most recent call last):
>   File "<pyshell#17>", line 1, in <module>
>     myfiles.myfiles('C:\\python26')
>   File "C:\Python26\lib\site-packages\myfiles.py", line 6, in myfiles
>     for i in z if i.endswith(extension)]:print x
> TypeError: expected a character buffer object
> >>> myfiles.myfiles('C:\\python26','doc')
> C:\python26\Doc\examples.doc
>
> How can I rectify this?
>
> Thanks 
>
> Prasad
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
Chapel Hill NC 
919-636-4239



More information about the Tutor mailing list