Rcjp's Weblog

February 1, 2007


Filed under: python, unix — rcjp @ 11:23 am

I hate globbing. Whoever thought that it was a good idea to let the shell snaffle the arguments you’ve given to a program needs to be pushed off somewhere high onto some burning cactii.

You can turn it off in the shell, but this makes most unix commands useless since they don’t do the file expansion themselves (like they should).

In ruby…

    for f in Dir.glob("/home/r/tmp/**/*")
        File.open(f) { |file|
            puts "filename #{f}"
        } if File.file?(f)


    import glob, os

    for root, dirs, files in os.walk('/home/r/tmp'):
        for f in files: print 'file -> ', os.path.join(root,f)

The Common Lisp pathname stuff can be a bit confusing, which is a pity because it does have some nice tricks up its sleeve.

You build the directory section of the pathname as a list taking keyword arguments :wild where you want to match at one directory level and :wild-inferiors when you want to recurse.

CL-USER> (make-pathname :name :wild
                         :type :wild
                         :directory (list :absolute "home" "r" "tmp" :wild-inferiors))

so running (DIRECTORY #P"/home/r/tmp/**/*.*") should give you all the files in the tmp directory and below. Some compilers though take directories themselves to be files and others don’t, so you might need to use FILE-NAMESTRING to see if the filename part of the path is nil i.e. its a directory.


Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: