Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Anything better than shutil?

Reply
Thread Tools

Anything better than shutil?

 
 
Roy Smith
Guest
Posts: n/a
 
      11-14-2009
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.
 
Reply With Quote
 
 
 
 
Chris Withers
Guest
Posts: n/a
 
      11-14-2009
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-15-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Anything better than Panel control? Shawn Repphan ASP .Net 1 11-01-2005 04:26 PM
Can digicams do anything better than film SLR's?? mark.worthington Digital Photography 15 04-18-2004 02:45 AM
Anything new better than Epson 2200 for quality, speed etc. Flierbk Digital Photography 10 09-20-2003 09:50 PM



Advertisments