Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anything better than shutil?

0 views
Skip to first unread message

Roy Smith

unread,
Nov 14, 2009, 10:48:39 AM11/14/09
to
I'm converting some old bash scripts to python. There's lots of
places where I'm doing things like "rm $source_dir/*.conf". The best
way I can see to convert this into python is:

configs = glob.glob(os.path.join(source_dir, '*.conf'))
for conf_file in configs:
shutil.copy(conf_file, conf_dir)

which is pretty clunky. The old bash script ran under cygwin on
windows, and the cygwin layer handled the slash-backslash business for
me. Is there a better way to do what I'm doing?

I don't want to use any of the popen() variants to call a real shell.
The problem I'm trying to solve is that fork/exec is painfully slow
under cygwin, so that would defeat the whole purpose.

The idea interface I see would be one like:

shutil.copy([source_dir, '*.conf'], conf_dir)

the idea is that if the first argument is a list (or maybe any
iterable other than a string?), it would automatically get run through
os.path.join(). And, the result would always get passed through glob
(), just like a normal shell would. Does anything like this exist?

I'm currently using python 2.5.1. It's possible, but moderately
painful, to move to a newer version.

Chris Withers

unread,
Nov 14, 2009, 10:54:51 AM11/14/09
to Roy Smith, pytho...@python.org
Roy Smith wrote:
> The idea interface I see would be one like:
>
> shutil.copy([source_dir, '*.conf'], conf_dir)
>
> the idea is that if the first argument is a list (or maybe any
> iterable other than a string?), it would automatically get run through
> os.path.join(). And, the result would always get passed through glob
> (), just like a normal shell would. Does anything like this exist?

Why don't you wrap up your earlier code:

> configs = glob.glob(os.path.join(source_dir, '*.conf'))
> for conf_file in configs:
> shutil.copy(conf_file, conf_dir)


...in a module with the exact interface you're after?

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Steven D'Aprano

unread,
Nov 14, 2009, 11:29:44 PM11/14/09
to
On Sat, 14 Nov 2009 07:48:39 -0800, Roy Smith wrote:

> I'm converting some old bash scripts to python. There's lots of places
> where I'm doing things like "rm $source_dir/*.conf". The best way I can
> see to convert this into python is:
>
> configs = glob.glob(os.path.join(source_dir, '*.conf'))
> for conf_file in configs:
> shutil.copy(conf_file, conf_dir)
>
> which is pretty clunky.

Particularly since you're converting a remove to a copy...

I suppose if you're used to the sort of terse code filled with magic
characters that you find in bash, then the Python code might seem a bit
verbose. And I suppose you would be right :) But trying to do something
complicated in bash rapidly becomes *far* more verbose, unreadable and
clunky than Python.

> The idea interface I see would be one like:
>
> shutil.copy([source_dir, '*.conf'], conf_dir)

Then write a helper function, and call that.

# Untested.
def copy(glb, destination):
if not isinstance(glb, str):
glb = os.path.join(*glb)
glb = glob.glob(glb)
for source in glb:
shutil.copy(source, destination)

--
Steven

0 new messages