perl to python

Andrew Dalke adalke at mindspring.com
Thu May 13 02:06:23 EDT 2004


> > Kirk Job-Sluder wrote:
> >> Write me a two-line script in python that reads a character delimited
> >> file, and printf pretty-prints all of the records in a different order.

Carl Banks wrote one, convoluted so it can be on the command line.
Kirk Job-Sluder replied
> This looks like using the proverbial hammer to drive the screw.

But you asked use to use the hammer to drive in the screw.  In real
life I have more tools to use.  For this case I would use Perl or awk.

Here's one for you.  I had several mailbox files arranged like
  Inbox.mbox/mbox
  Send.mbox/mbox
  OBF&BOSC.mbox/mbox
  Work Email.mbox/mbox

I wanted to raise the "*/mbox" files one directory so that
  Inbox.mbox/mbox    --becomes--> Inbox.mbox

My solution was to use the interactive Python shell.  Something
like (untested)

import glob, os
filenames = glob.glob("*.mbox")
for name in filenames:
  os.rename(name + "/mbox", "."+name)
  os.rmdir(name)
  os.rename("."+name, name+".mbox")

Trying doing that sanely with any programming language expressed
all on the command-line.  No credit if you can't handle the '&' and space.

                    Andrew
                    dalke at dalkescientific.com





More information about the Python-list mailing list