Wednesday, January 18, 2012

[Level 3] Run python in shell script.

If you want to wrap python script into shell script.
You could use the following sample code.
#!/bin/sh
# -*- mode: Python -*-

""":"
# bash code here; finds a suitable python interpreter and execs this file.
# prefer unqualified "python" if suitable:
python -c 'import sys; sys.exit(sys.hexversion < 0x020500b0)' 2>/dev/null \
    && exec python "$0" "$@"
for pyver in 2.6 2.7 2.5; do
    which python$pyver > /dev/null 2>&1 && exec python$pyver "$0" "$@"
done
echo "No appropriate python interpreter found." >&2
exit 1
":"""

import os, sys 

print os.path.dirname('/Hello/World/c.txt')

$ ./test.sh
/Hello/World
Wish this helps. regards, Stanley Huang