Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > More elegant way to cwd?

Reply
Thread Tools

More elegant way to cwd?

 
 
Kamilche
Guest
Posts: n/a
 
      12-24-2004
Is there a more elegant way to change the working directory of Python
to the directory of the currently executing script, and add a folder
called 'Shared' to the Python search path?

This is what I have. It seems like it could be shorter, somehow.

# Switch Python to the current directory
import os, sys
pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname)
s = os.path.realpath('..')
s = os.path.join(s, 'Shared')
sys.path.append(s)

Thanks for any enhancements you can suggest!

 
Reply With Quote
 
 
 
 
M.E.Farmer
Guest
Posts: n/a
 
      12-24-2004
Hello,
Seems you found os.chdir(), but you missed os.getcwd()

M.E.Farmer

 
Reply With Quote
 
 
 
 
M.E.Farmer
Guest
Posts: n/a
 
      12-24-2004
Ignore my last post to much eggnog I guess. I must have parsed it to
quickly.

M.E.Farmer

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      12-27-2004
Kamilche wrote:
> Is there a more elegant way to change the working directory of Python


That depends on how you define "elegant", I guess.

> to the directory of the currently executing script, and add a folder
> called 'Shared' to the Python search path?
>
> This is what I have. It seems like it could be shorter, somehow.


It could be shorter if you were willing to combine several
function calls on the same line, but I get the impression
you wouldn't consider that more elegant...

> # Switch Python to the current directory
> import os, sys
> pathname, scriptname = os.path.split(sys.argv[0])
> pathname = os.path.abspath(pathname)
> os.chdir(pathname)
> s = os.path.realpath('..')
> s = os.path.join(s, 'Shared')
> sys.path.append(s)
>
> Thanks for any enhancements you can suggest!


Other than using os.pardir instead of '..', and possibly adding
an "os.path.abspath()" call to the last bit (or does realpath
already do that? It's unclear from the docs), I can't see
anything fundamental I'd do differently... except package these
functions up as nice clean subroutines, possibly in a library
package, that I could then use in code that would magically
become "elegant" (IMHO) by avoiding the repetition of all
that non-elegant stuff above...

-Peter
 
Reply With Quote
 
F. Petitjean
Guest
Posts: n/a
 
      12-27-2004
On Mon, 27 Dec 2004 11:53:57 -0500, Peter Hansen <(E-Mail Removed)> wrote:
> Kamilche wrote:
>> Is there a more elegant way to change the working directory of Python

>
> That depends on how you define "elegant", I guess.
>
>> to the directory of the currently executing script, and add a folder
>> called 'Shared' to the Python search path?
>>
>> This is what I have. It seems like it could be shorter, somehow.

>
> It could be shorter if you were willing to combine several
> function calls on the same line, but I get the impression
> you wouldn't consider that more elegant...
>
>> # Switch Python to the current directory
>> import os, sys
>> pathname, scriptname = os.path.split(sys.argv[0])
>> pathname = os.path.abspath(pathname)
>> os.chdir(pathname)
>> s = os.path.realpath('..')
>> s = os.path.join(s, 'Shared')
>> sys.path.append(s)
>>
>> Thanks for any enhancements you can suggest!

>

How about ?
import os, sys
pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname) # only if you really need to cwd
parent = os.path.dirname(pathname)
parent = os.path.join(parent, 'Shared')
if parent not in sys.path:
sys.path.append(parent)


Or even :
pathname, scriptname = os.path.split(__file__)
# pathname should be an absolute path
 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      12-28-2004
Peter Hansen wrote:
[snip]
> Other than using os.pardir instead of '..', and possibly adding
> an "os.path.abspath()" call to the last bit (or does realpath
> already do that? It's unclear from the docs)

[snip]
I believe os.path.abspath and os.path.realpath are the same.
realpath is just an alias for abspath.
Using pydoc on os gives this:

py>realpath = abspath(path)
py> Return the absolute version of a path

M.E.Farmer

 
Reply With Quote
 
Kamilche
Guest
Posts: n/a
 
      12-28-2004
> Other than using os.pardir instead of '..', and possibly adding
> an "os.path.abspath()" call to the last bit (or does realpath
> already do that? It's unclear from the docs), I can't see
> anything fundamental I'd do differently... except package these
> functions up as nice clean subroutines, possibly in a library
> package, that I could then use in code that would magically
> become "elegant" (IMHO) by avoiding the repetition of all
> that non-elegant stuff above...
>
> -Peter


Well... but to call it from the shared directory, I'd have to first
switch to the shared directory! Which would defeat the purpose.

I wish there was a way to store this in a single file and call it from
any script, but I'm unwilling to keep all my scripts in one folder to
accomodate it. If only Python would let you import modules from
different directories. :-/

--Kamilche
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      12-28-2004
Kamilche wrote:

>> Other than using os.pardir instead of '..', and possibly adding
>> an "os.path.abspath()" call to the last bit (or does realpath
>> already do that? It's unclear from the docs), I can't see
>> anything fundamental I'd do differently... except package these
>> functions up as nice clean subroutines, possibly in a library
>> package, that I could then use in code that would magically
>> become "elegant" (IMHO) by avoiding the repetition of all
>> that non-elegant stuff above...
>>
>> -Peter

>
>
> Well... but to call it from the shared directory, I'd have to first
> switch to the shared directory! Which would defeat the purpose.
>
> I wish there was a way to store this in a single file and call it from
> any script, but I'm unwilling to keep all my scripts in one folder to
> accomodate it. If only Python would let you import modules from
> different directories. :-/
>
> --Kamilche


Maybe I'm misunderstanding your requirements, but it seems to me
perfectly practical to keep that script in a standard place (say,
lib/site-packages, which was intended for exactly that purpose) and
import it from there. There's no reason why its location has to relate
to the location of the scripts that import it and use that function from it.

What am I missing?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      12-28-2004
M.E.Farmer wrote:
> Peter Hansen wrote:
> [snip]
>> Other than using os.pardir instead of '..', and possibly adding
>> an "os.path.abspath()" call to the last bit (or does realpath
>> already do that? It's unclear from the docs)

> [snip]
> I believe os.path.abspath and os.path.realpath are the same.
> realpath is just an alias for abspath.


Yes, on Windows. On UNIX systems, realpath resolves symbolic links while
abspath doesn't.

mfg
Georg
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      12-28-2004
Kamilche wrote:
> Well... but to call it from the shared directory, I'd have to first
> switch to the shared directory! Which would defeat the purpose.


As Steve said, plus "use PYTHONPATH or .pth files". That's
exactly what they're for.

Doing what you're doing as a means of getting access to
regularly used shared modules makes no sense. Within an
application, to get access to a particular, special set of
modules for some reason, that can often make sense. For
"library" modules there are already other and more elegant
means.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
More elegant way to try running a function X times? Gilles Ganault Python 9 11-20-2008 09:18 PM
More elegant way to obtain ACLs / permissions for windows directories than using "cacls" dos command? dananrg@yahoo.com Python 5 11-09-2006 04:20 PM
There's got to be a more elegant way Lilith C++ 8 09-02-2005 06:34 AM
Is there a more elegant way to do this? Kamilche Python 7 06-29-2004 04:33 AM
Looking for a more elegant way to do memory offsets mikegw C Programming 5 05-24-2004 11:39 AM



Advertisments