By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

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

P: n/a

[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

Oct 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Oct 15 '05 #2

P: n/a
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>
Oct 15 '05 #3

P: n/a
"Chris F.A. Johnson" <cf********@gmail.com> 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

Oct 15 '05 #4

P: n/a
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Oct 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.