473,770 Members | 4,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.basenam e(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.comp ile(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 2058
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.basenam e(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.comp ile(file);

--
=============== =============== =============== =============== =======
Maarten van Reeuwijk dept. of Multiscale Physics
Phd student Faculty of Applied Sciences
maarten.ws.tn.t udelft.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.basenam e(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.comp ile(file);
' $*
}
Don't indent:

function compile ()
{
python -c '
import os, sys, py_compile;
i = 0;
for arg in sys.argv:
file = os.path.basenam e(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.comp ile(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********@gma il.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.basenam e(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.comp ile(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
3373
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 features to them. "Of course," I'm redoing them in python - much of the cut&paste reuse has become common functions, which then get made more robust and have a common style and are callable from other (python) tools directly, instead of having to...
1
1659
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 using a for loop to render a serie of images, which are rendered | filtered | composited > to an image The problem is that instead of a single pipeline 2 are being executed in
7
2986
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? Thanks!
9
14217
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 script is fetching from a file using grep command but the ftp log says the script is trying to logon as user 'aginvprint' Looks like the script is removing the character '\' when trying to ftp Can someone please help and let me know how to...
2
2962
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 output to a file (which we are refering as a log). python TestInvoiceTransform.py -glUserGid <user> -glPassword <password> -inputFileName TestInvoice.csv -hostname <host url> -mailto <mail id> -smtphost amermsx.med.ge.com > test.log The output is...
2
2619
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 there are several hundred), and each node is in a separate directory. I am attempting to extract the node name and IP address from each .def file and compile these into a single file, but I'm getting an error, no matter what I alter, so I think...
16
9159
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 create the executable. What is the difference between this method and writing a make file ?
10
3455
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 normally available to the script. Actually, it works with bash scripts but not with python scripts import os files = os.environ.splitlines()
10
4045
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 matching word in file2, then edit that line in file2, the third column in file1 in that row tells that column should edit in file2, basically third column tells which column should edit in file, the fourth column in file1 is the replacement in file2 in...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.