473,408 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

More elegant way to cwd?

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!

Jul 18 '05 #1
6 4993
Hello,
Seems you found os.chdir(), but you missed os.getcwd()
;)
M.E.Farmer

Jul 18 '05 #2
Ignore my last post to much eggnog I guess. I must have parsed it to
quickly.

M.E.Farmer

Jul 18 '05 #3
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
Jul 18 '05 #4
On Mon, 27 Dec 2004 11:53:57 -0500, Peter Hansen <pe***@engcorp.com> 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
Jul 18 '05 #5
> For "library" modules there are already other and more elegant means.

-Peter


Well, I want to make a script execute without error regardless of where
the current working directory is, and without having to make the user
modify their PYTHONPATH variable or install something in site-packages.

Is there a way to let the user just 'drag the folder' anywhere on their
hard drive, and have it work, without having to modify any special
system variables?
Jul 18 '05 #6
Kamilche wrote:
For "library" modules there are already other and more elegant means.
Well, I want to make a script execute without error regardless of where
the current working directory is, and without having to make the user
modify their PYTHONPATH variable or install something in site-packages.


If you are talking about just "a script", then this of course
works out of the box without doing anything special. You just
run the script and it works.

If, on the other hand, the script requires access to certain
shared *other* modules, then no, if you don't tell the script
where to find those other modules, and they are not in the same
folder as the script itself, then you can't do this. You do
have to tell the Python where to find things... I'm unclear
what your situation is, that you have shared modules that you
want to use in various other scripts, and yet you have some
strange restriction preventing you from telling Python where
those are to be found except with custom code written in each
script that will use the modules.

Even if that really describes your (very bizarre, IMHO) situation,
the least you could do is write the aforementioned code in a
nice subroutine that you just cut and paste into each module,
then call "elegantly". It's code reuse, even if it's the most
rudimentary sort of reuse.
Is there a way to let the user just 'drag the folder' anywhere on their
hard drive, and have it work, without having to modify any special
system variables?


Not sure exactly what you are looking for. I certain have folders
of Python scripts that I can just drag here or there and have them
work. When they need to use common library files, however, I take
a moment to set that up ahead of time, using .pth files if I can't
or won't install them in site-packages.

-Peter
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hey, well I'm really pleased with myself for writing a little script today that changes variables according to a radio button. The thing is, I have this feeling part of my script could be a lot...
4
by: Kamilche | last post by:
''' Is there a more elegant way of doing this? I would like to have the arguments to pack automatically taken from lst. ''' def pack(self): lst = return struct.pack('<IIIiiiBBBHHH', \...
17
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
13
by: Edward W. | last post by:
hello, I have this function below which is simple and easy to understand private function ListHeight (byval UserScreenHeight as int) as int if UserScreenHeight < 1024 return 30 else return 50...
4
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an...
10
by: sherifffruitfly | last post by:
Hi all, This is how I'm currently getting Friday of last week. It strikes me as cumbersome. Is there a slicker/more elegant way? Thanks for any ideas, cdj
35
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
3
by: Anonymous | last post by:
I want to be able to restrict the set of classes for which a template class can be instantiated (i.e enforce that all instantiation MUST be for classes taht derive from a base type BaseType). ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.