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

J

Jari Aalto

[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
 
M

Maarten van Reeuwijk

Jari said:
[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);
 
C

Chris F.A. Johnson

[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);
' $*
}
 
J

Jari Aalto

Chris F.A. Johnson said:
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
 
R

Robert Kern

Jari said:
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
(e-mail address removed)

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

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top