473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


[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
4 2031
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: eichin | last post by:
One of my recent projects has involved taking an accretion of sh and perl scripts and "doing them right" - making them modular, improving the error reporting, making it easier to add even more...
1
by: Jelle Feringa // EZCT / Paris | last post by:
James, Terry, Thanks for your constructive comments. Let's see if we can come down to the heart of the matter, it seems to me these are fundamental issues relevant in any shell script. I'm...
7
by: spec | last post by:
Hi all, I know nothing about Python. What I need to do is to get a Python script to execute a local shell script. I do not need any output. What would be th eeasiest way to accomplish this? ...
9
by: niteck07 | last post by:
I am using following shell script to ftp files to another server but this is failing as the shell script changes the user name for the ftp login the correct user name is 'ag\invprint' which the...
2
by: jallam | last post by:
Hi, I have a Python script to upload the Invoice data which is in .csv file to OTM (GLOG) system. I'm running this script using the command(given below) using a shell script and capturing the...
2
Colloid Snake
by: Colloid Snake | last post by:
Hello everyone- I am working on a shell script to parse out a Node.def file that contains the hostname, version, IP address, and date updated. There is a separate .def file for each node (of which...
16
by: pereges | last post by:
Do you see anything wrong about this method ? For eg. I write a shell script a.sh containing : cc -o test file1.c file2.c file3.c and then execute the shell script ( sh a.sh) to compile and...
10
by: Michel Leunen | last post by:
Hi, I'm trying to write a python script for Nautilus. To get the list of files selected in the Nautilus right pane, you use the $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS environment variable which is...
10
by: tvnaidu | last post by:
Looking for shell script to modify file2 below based on first file. Basically I need to look for rows start with "TAPES" in file1, then I have to take 2nd column in that line "LOC" and look for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.