Tag Archive for 'linux'

Compiling Python Scripts

Not too sure how much this would help in debugging python scripts, but at least it tells you any syntax errors before a long script runs for an hour before throwing an error.

Here’s the code

# compile.py
import py_compile, sys
py_compile.compile(sys.argv[1])

Here’s a sample python script

# helloworld.py
print “Hello World!”

Here’s an example to compile it
$ python ./compile.py ./helloworld.py

Running this generates a bytecode file called helloworld.pyc which could be run
$ python ./helloworld.pyc

python could also be used with the flag “-O” for optimize generated bytecode

>>> exit()

Shell Script for Looping Each Directory

Let’s say you are creating a batch script to loop through each “folder” you have in an directory- run:

$./list_dirs.sh /app

inside the shell script:
#list_dirs.sh
for i in $(ls -l $1 | awk ‘/^d/ { print $NF }’) ; do
echo $i # replace with actions to be done
done
#end script

basically ls -l will display all files+folders with their attributes.
awk filters to those lines starting with d attribute (/!d/) and strips out to the last field ($NF)

blog>logout