hpw to convert a linux python script ?

John Machin sjmachin at lexicon.net
Wed Feb 11 16:25:07 EST 2009


On Feb 12, 7:48 am, Stef Mientki <stef.mien... at gmail.com> wrote:
> Alec Schueler wrote:
> > On Feb 11, 7:58 pm, Stef Mientki <stef.mien... at gmail.com> wrote:
>
> >> As there are a whole lot of these lines, in a whole lot of files,
> >> I wonder if there's a simple trick to point
> >>   /usr/share/tinybldLin/
> >> to my directory ?
>
> >> thanks,
> >> Stef
>
> > Find and replace?
>
> well I was thinking of a more elegant way,
> so the scripts remains, or better become multi-platform.

Your current setup is not even single-platform portable ... consider
the scenario where some Linux admin wants your gear to live in some
directory other than /usr/share/tinybldLin/

The standard technique for ensuring that kind of portability involves:
1. Find the directory in which your script lives. Inspect sys.argv[0],
use some os.path.some_functionality() to extract the directory, name
it (say) home_base.
2. When you want to open a data file in the script's directory, do
this:
   the_path = os.path.join(home_base, 'stef1.dat')
   f = open(the_path, .....)

NOTE: Use os.path.join() instead of some DIY technique; it knows
better than you what the path separator is on the OS it's being run
on. In fact, read the whole of the os.path manual carefully before you
jump in to changing your code.

If you prefer to have the data files in (say) .../script_locn/data
instead of .../script_locn, the adjustment should be obvious.

Unfortunately, you are still going to need use find and replace to fix
your script. Code in haste, repent at leisure :-)

HTH,
John






More information about the Python-list mailing list