473,398 Members | 2,368 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,398 software developers and data experts.

How to read the directory which the actively running python file islocated in?

Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.

Dec 1 '06 #1
4 1809
in os module there is many funktion/methods to extract this information
to ask the path to the current running pythonprogram you can do likes
this

----- CUT-----------
import os
print os.getcwd()

----- CUT ------

// Anders
Michael Malinowski skrev:
Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.
Dec 1 '06 #2
in os module there is many funktion/methods to extract this information
to ask the path to the current running pythonprogram you can do likes
this

----- CUT-----------
import os
print os.getcwd()

----- CUT ------

// Anders
Michael Malinowski skrev:
Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.
Dec 1 '06 #3
Michael Malinowski schrieb:
Is there a way to read the directory that the currently running python file
is located in?
Hi Mike!

To get the started program:

sys.argv[0]

Donīt use ``os.curdir``.
To get the filename, of the current module:

__file__
To get the directory:

os.path.split(sys.argv[0])[0]
os.path.split(__file__)[0]
Regards,
Gerold
:-)

--
__________________________________________________ ______________________
Gerold Penz - bcom - Programmierung
ge*********@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
Dec 1 '06 #4

anders wrote:
in os module there is many funktion/methods to extract this information
to ask the path to the current running pythonprogram you can do likes
this

----- CUT-----------
import os
print os.getcwd()

----- CUT ------

// Anders
Michael Malinowski skrev:
Is there a way to read the directory that the currently running python file
is located in?
Cheers
Mike.
os.getcwd() provides the "current working directory". This is *not*
necessarily the directory that the "currently running python file"
[whatever that means] is located in.

Try picking what you really want/need out of this:

C:\junk\foo>type ..\where.py
import sys, os
if __name__ == "__main__":
print "running as script"
else:
print "imported module named", __name__
print "code loaded from file", __file__
print "sys.argv[0] is", sys.argv[0]
print "cwd is", os.getcwd()

C:\junk\foo>..\where.py
running as script
code loaded from file C:\junk\where.py
sys.argv[0] is C:\junk\where.py
cwd is C:\junk\foo

C:\junk\foo>type runwhere.py
import sys
sys.path[0:0] = ['c:\\junk']
import where

C:\junk\foo>runwhere.py
imported module named where
code loaded from file c:\junk\where.py
sys.argv[0] is C:\junk\foo\runwhere.py
cwd is C:\junk\foo

C:\junk\foo>cd ..

C:\junk>md bar

C:\junk>cd bar

C:\junk\bar>..\foo\runwhere.py
imported module named where
code loaded from file c:\junk\where.pyc
sys.argv[0] is C:\junk\foo\runwhere.py
cwd is C:\junk\bar

HTH,
John

Dec 1 '06 #5

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

Similar topics

5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
1
by: Chris | last post by:
I need to monitor a bunch of files to see what is being opened, read, and closed, with no modifications being made. This is being done first on Windows machines, and later on Solaris. Looking...
3
by: Larry Bates | last post by:
I had occasion to look back at a project I did over a year ago and needed to make one small change. I use py2exe to package it for distribution via Inno Setup. After making my change I tried to...
2
by: francescomoi | last post by:
Hi. I'm trying to build 'MySQL-python-1.2.0' on my Linux FC2: ---------------------------------- # export PATH=$PATH:/usr/local/mysql/bin/ # export mysqlclient=mysqlclient_r # python setup.py...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
2
by: Samuel Yin | last post by:
Hi, guys, This should be a simple problem, but I just can not resolve it. I just want to use a python script to change my working directory. see my following code: # mycd.py 1) destdir =...
2
by: rrossney | last post by:
Please look at the "what I've already done" section of this message before responding to it: I believe that I've done everything that the people who experience this error are typically told to do....
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
1
by: Steven D'Aprano | last post by:
On Wed, 22 Oct 2008 16:59:45 -0400, Terry Reedy wrote: With respect Terry, I think what you have missed is the reason why the OP thinks this is a bug. He's not surprised that buffering is going...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.