Python - Haskell comparison: delete all files matching *m3u

Submitted by metaperl on Fri, 11/03/2006 - 5:24am.

Python is a not-bad language... much nicer than Perl. Not as rigid as Haskell. Many more libraries and applications than Haskell. And Python was used in this years winning ICFP as well. And Python stole list comprehensions for Python. And PYthon combined object, functional and imperative in a prettier more understandable way than Ocaml did.

Here is a script I whipped up in Python to remove all m3u files from my music dir... the shell 1-liner of

find ~/Music -name '*m3u' | xargs rm

failed because find did not properly escape spaces and parentheses.

So here's my script:

#!/usr/bin/env python

from path import path

p = path('/Users/tbrannon/Music')
print p

dirs = p.walkdirs('*')
print dirs

for d in dirs:
for f in d.files('*m3u'):
print f

What would the Haskell look like?

Submitted by lispy on Fri, 11/03/2006 - 5:31am.

I can't think of a 2 liner off the top of my head to do this in haskell, but that's not the point.

Of course, being able to do simple things easily is important, but the real win with Haskell is in maintainability and in proving correctness. For this type of problem, proving correctness is overkill. So, perhaps haskell is too, ya know?

(Note: a 1 Liner in haskell probably exists if I looked for it, but like I said, that's not the point.)

Submitted by graham.fawcett on Fri, 11/03/2006 - 8:19am.

Well, I can think of a one-liner; but not in Haskell:-)

find ~/Music -name "*m3u" -exec rm {} \;

or, if you really want to use xargs:

find ~/Music -name "*m3u" -printf '"%p" ' | xargs rm
Submitted by graham.fawcett on Fri, 11/03/2006 - 8:25am.

Wait a sec. I just noticed, you're using Jason Orendorff's path module in your Python script; readers should note that this (excellent) module is not part of the Python standard library. With path, you could shorten your script down to a one-liner in Python:

[f.remove() for f in path('/Users/tbrannon/Music').walkfiles('*.m3u')]

It has the side effect of evaluating to a list of the return-values of your 'remove' calls; there are ways around this, but if that's a real concern, better to write it as a two-liner:

for f in path('/Users/tbrannon/Music').walkfiles('*.m3u'):
Submitted by jgoerzen on Fri, 11/03/2006 - 10:44am.
The correct shell incantation is:

find ~/Music -print0 -name '*m3u' | xargs -0 rm

In Haskell, if you have MissingH:

import MissingH.Path
import Data.List
import System.Directory

main = recurseDir "Music" >>= (mapM_ removeFile . filter (isSuffixOf "m3u"))

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.