Anything better than shutil?

Discussion in 'Python' started by Roy Smith, Nov 14, 2009.

  1. Roy Smith

    Roy Smith Guest

    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.
    Roy Smith, Nov 14, 2009
    #1
    1. Advertising

  2. 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
    Chris Withers, Nov 14, 2009
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Nov 15, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Repphan

    Anything better than Panel control?

    Shawn Repphan, Nov 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Mythran
    Nov 1, 2005
  2. Alessandro Bottoni

    Is there anything "better" than impalib/poplib?

    Alessandro Bottoni, Sep 7, 2005, in forum: Python
    Replies:
    1
    Views:
    253
    Thomas Guettler
    Sep 7, 2005
  3. Peter Bencsik
    Replies:
    2
    Views:
    824
  4. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    871
    alex23
    Sep 26, 2011
  5. Paganoni
    Replies:
    9
    Views:
    118
    Gregory Brown
    Apr 2, 2009
Loading...

Share This Page