Anything better than shutil?

R

Roy Smith

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.
 
C

Chris Withers

Roy said:
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
 
S

Steven D'Aprano

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top