how to improve simple python shell script (to compile list of files)

Discussion in 'Python' started by Jari Aalto, Oct 15, 2005.

  1. Jari Aalto

    Jari Aalto Guest

    [Keep CC, thank you]

    Please suggest comments how can I make this script to work
    from bash. Also how can I skip better the [0] argument from
    command line without hte extra variable i?

    #!/bin/bash

    function compile ()
    {
    python -c '
    import os, sys, py_compile;
    i = 0;
    for arg in sys.argv:
    file = os.path.basename(arg);
    dir = os.path.dirname(arg);
    i += 1;
    if i > 1 and os.path.exists(dir):
    os.chdir(dir);
    print "compiling %s\n" % (file);
    py_compile.compile(file);
    ' $*
    }

    compile $(find path/to -type f -name "*.py")

    # End of example

    The error message reads:

    File "<string>", line 2
    import os, sys, py_compile;
    ^
    SyntaxError: invalid syntax

    Jari
    Jari Aalto, Oct 15, 2005
    #1
    1. Advertising

  2. Jari Aalto wrote:

    >
    > [Keep CC, thank you]
    >
    > Please suggest comments how can I make this script to work
    > from bash. Also how can I skip better the [0] argument from
    > command line without hte extra variable i?


    Didn't check, but something like this?

    #!/bin/python
    import os, sys, py_compile;
    i = 0;
    for arg in sys.argv:
    file = os.path.basename(arg);
    dir = os.path.dirname(arg);
    i += 1;
    if i > 1 and os.path.exists(dir):
    os.chdir(dir);
    print "compiling %s\n" % (file);
    py_compile.compile(file);

    --
    ===================================================================
    Maarten van Reeuwijk dept. of Multiscale Physics
    Phd student Faculty of Applied Sciences
    maarten.ws.tn.tudelft.nl Delft University of Technology
    Maarten van Reeuwijk, Oct 15, 2005
    #2
    1. Advertising

  3. On 2005-10-15, Jari Aalto wrote:
    >
    > [Keep CC, thank you]
    >
    > Please suggest comments how can I make this script to work
    > from bash. Also how can I skip better the [0] argument from
    > command line without hte extra variable i?
    >
    > #!/bin/bash
    >
    > function compile ()
    > {
    > python -c '
    > import os, sys, py_compile;
    > i = 0;
    > for arg in sys.argv:
    > file = os.path.basename(arg);
    > dir = os.path.dirname(arg);
    > i += 1;
    > if i > 1 and os.path.exists(dir):
    > os.chdir(dir);
    > print "compiling %s\n" % (file);
    > py_compile.compile(file);
    > ' $*
    > }


    Don't indent:

    function compile ()
    {
    python -c '
    import os, sys, py_compile;
    i = 0;
    for arg in sys.argv:
    file = os.path.basename(arg);
    dir = os.path.dirname(arg);
    i += 1;
    if i > 1 and os.path.exists(dir):
    os.chdir(dir);
    print "compiling %s\n" % (file);
    py_compile.compile(file);
    ' $*
    }

    > compile $(find path/to -type f -name "*.py")
    >
    > # End of example
    >
    > The error message reads:
    >
    > File "<string>", line 2
    > import os, sys, py_compile;
    > ^
    > SyntaxError: invalid syntax
    >
    > Jari
    >



    --
    Chris F.A. Johnson <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>
    Chris F.A. Johnson, Oct 15, 2005
    #3
  4. Jari Aalto

    Jari Aalto Guest

    Re: how to improve simple python shell script (to compile list offiles)

    "Chris F.A. Johnson" <> writes:

    > On 2005-10-15, Jari Aalto wrote:
    > Don't indent:
    >
    > function compile ()
    > {
    > python -c '
    > import os, sys, py_compile;
    > i = 0;
    > for arg in sys.argv:
    > file = os.path.basename(arg);
    > dir = os.path.dirname(arg);
    > i += 1;
    > if i > 1 and os.path.exists(dir):
    > os.chdir(dir);
    > print "compiling %s\n" % (file);
    > py_compile.compile(file);
    > ' $*
    > }


    Thanks, is there equivalent to this Perl statement in Python?

    @list = @ARGV[1 .. @ARGV];

    or something similar so that I could avoid the 1 > 1 (sys.argv) check
    altogether?

    Jari
    Jari Aalto, Oct 15, 2005
    #4
  5. Jari Aalto

    Robert Kern Guest

    Re: how to improve simple python shell script (to compile list offiles)

    Jari Aalto wrote:

    > Thanks, is there equivalent to this Perl statement in Python?
    >
    > @list = @ARGV[1 .. @ARGV];
    >
    > or something similar so that I could avoid the 1 > 1 (sys.argv) check
    > altogether?


    for arg in sys.argv[1:]:
    ...

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Oct 15, 2005
    #5
    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. Christian Heimes
    Replies:
    0
    Views:
    583
    Christian Heimes
    Feb 27, 2008
  2. Gerardo Herzig
    Replies:
    1
    Views:
    1,058
    Philipp Pagel
    Feb 27, 2008
  3. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    844
    D'Arcy J.M. Cain
    Feb 27, 2008
  4. Samuel A. Falvo II
    Replies:
    0
    Views:
    473
    Samuel A. Falvo II
    Oct 8, 2008
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    236
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page