[Tutor] files in a directory

Chad Crabtree flaxeater at yahoo.com
Sun Jan 30 09:53:37 CET 2005


Well in the same vein as what the others put out there I made a
verbose 
'ls *.ext' so that you can see how to do it in one go.  I figured
this 
would give you enough of an example.  You can hard code these things 
into your program.  I used a construct similar to this to create an 
instant html photo gallery.

#!/usr/bin/env python
import sys,os
print sys.argv
if len(sys.argv)<3:
        print """
Please type dir.py dir=<directory> ext=<extension>
Example
dir.py dir=. ext=html
To list all the html files in the current directory"""
        sys.exit()
dir=sys.argv[1]
ext=sys.argv[2]
dir=dir.split('=')
ext=ext.split('=')
if not (dir[0].lower()=='dir' and ext[0].lower()=='ext'):
        print """
Invalide Command line systax
dir.py dir=<directory> ext=<extension>
        """
        sys.exit()
#now we can get started
dirlist=os.listdir(dir[1])
dirlist=[tuple(x.split('.')) for x in dirlist]
for x in dirlist:
        if x[-1]==ext[1]:
                print '.'.join(x)


kumar s wrote:

>f2.close()
>
>
>The problem with this approach is that, i have to give
>on file per a run. I have 50 files to pare and i want
>to do that in one GO.  I kepy those 50 files in one
>directory. Can any one suggest an approach to automate
>this process. 
>
>I tried to use f1 = stdin(...) it did not work. i dont
>know , possible is that i am using incorrect syntax.
>
>Any suggestions. 
>
>Thank you. 
>


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250


More information about the Tutor mailing list