If - Or statements

bruno modulix onurb at xiludom.gro
Mon Jun 6 04:13:03 EDT 2005


Ognjen Bezanov wrote:
(snip)

> filelist = os.listdir('/mnt/cdrom/') #get a list of files from the cdrom
> drive

<OT>
Please put long comments on the previous line... And BTW, please avoid
useless comments that just paraphrase the code
</OT>

>         for thefile in filelist[:]:   #for each file in the filelist

Do you really need to work on a copy of the list ?

>             if thefile.find(".") != -1:   #if the file has an extenstion
> at all
>                 ext = thefile.split('.') #get the file extension
>                 ext[1] =  ext[1].lower() #convert to lowercase

you may want to read the doc of the os.path module. The
os.path.splitext() function could save you a lot of work here.

>                 print ext[1] #debugging, to see the variable before
> passed to if statement
> 
>                 if ext[1] == "mp3" or ext[1] == "mp4" or ext[1] == "ogg"
> or ext[1] == "aac" or ext[1] == "wma":
>                     print "we have a valid extension: " + ext[1] #here
> would go the code for decoding the above
>                     pass
> 

Here's a somewhat more pythonic version:

filelist = os.listdir(path)
for filename in filelist:
    # I assume you want to remember what's the ext is
    ext = os.path.splitext(filename)
    # debug trace
    print "filename : %s - ext : %s" % (filename, ext)
    if ext in ['.this', '.that', '.whatnot']:
        print "ext is %s - should process this file" % ext


HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list