[Tutor] making proigress [using sudo with Python scripts]
Magnus Lycka
magnus@thinkware.se
Thu Dec 19 05:17:01 2002
At 01:25 2002-12-19 -0500, Kirk Bailey wrote:
>But clarify a issue for me. Here is an error from the log:
>f1 = open('./lists/aliases.tinylist','r') #180 open the aliases file
>IOError: [Errno 2] No such file or directory: './lists/aliases.tinylist'
>
>strange. Does open not accept relative path names?
It certainly does, but it can only open existing files for reading.
>I suppose I COULD whip up the absolute path, other scripts do, but will
>this not accept relative path declarations?
Put something like "print os.getcwd()" (you might need to print to a
log file perhaps?) before the open(). Relative paths are relative from
the location that the executing process considers to be its current
working directory... This is probably not what you think--and it could
change if the system is configured differently.
I suggest that you set some variable
BASE_DIR = '/some/path'
in the beginning of your script, and then either do
os.chdir(BASE_DIR)
before you start messing with files, or join your BASE_DIR with the
relative names when you open.
aliasesFile = file(os.path.join(BASE_DIR, 'lists/aliases.tinylist', 'r')
This way you can relocate your files by just changing
BASE_DIR. Of course, you could also read BASE_DIR from
a config file or environment variable etc.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se