473,396 Members | 1,703 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,396 software developers and data experts.

The folder a script is executed in

Hi,

How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -

myLocation = GetMyLocation()
print myLocation
>C:/folder
Thanks,

Aine.

Aug 21 '07 #1
7 1085
Ant
On Aug 21, 10:10 am, aine_ca...@yahoo.com wrote:
....
myLocation = GetMyLocation()
print myLocation
C:/folder
Do you mean the folder containing the script? Or the current working
directory?

If the former, then look at os.path.split(sys.argv[0])[0]
If the latter, try something like: os.path.abspath(os.curdir)

--
Ant...

http://antroy.blogspot.com/
Aug 21 '07 #2
Ant wrote:
Do you mean the folder containing the script? Or the current
working directory?

If the former, then look at os.path.split(sys.argv[0])[0]
test.py:
| #!/usr/bin/env python
| import sys,os
| print os.path.split(sys.argv[0])[0]

$ cd tmp
~/tmp$ ../test.py
...
~/tmp$

That's rather not what's intended. I'd try os.path.abspath(__file__)
instead.

Regards,
Björn

--
BOFH excuse #113:

Root nameservers are out of sync

Aug 21 '07 #3
Ant
On Aug 21, 10:47 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
Ant wrote:
....
| print os.path.split(sys.argv[0])[0]

$ cd tmp
~/tmp$ ../test.py
..
~/tmp$

That's rather not what's intended. I'd try os.path.abspath(__file__)
instead.
Fair point. On Win32 sys.argv[0] always seems to give the full path
rather than the relative path - hadn't realised it was different for
linux (?).

--
Ant...

http://antroy.blogspot.com/
Aug 21 '07 #4
Ant wrote:
Fair point. On Win32 sys.argv[0] always seems to give the full
path rather than the relative path
Strange.
- hadn't realised it was different for linux (?).
Yes, I used GNU/Linux for the example.

Regards,
Björn

--
BOFH excuse #148:

Insert coin for new game

Aug 21 '07 #5
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:
Hi,

How do I find out what folder a script is in while it is executing?

For example, for the file "C:/folder/script.py" contain the following
two lines of code -

myLocation = GetMyLocation()
print myLocation
def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
>
C:/folder

Thanks,

Aine.

Aug 21 '07 #6
On Aug 21, 10:23 am, Benjamin <musiccomposit...@gmail.comwrote:
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:Hi,
How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -
myLocation = GetMyLocation()
print myLocation

def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
>C:/folder
Thanks,
Aine.
As I understand it, this is one portable way to do it:

path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0]),
'.'))

Mike

Aug 21 '07 #7
On Aug 21, 10:23 am, Benjamin <musiccomposit...@gmail.comwrote:
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:Hi,
How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -
myLocation = GetMyLocation()
print myLocation

def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
never mind that above
def GetMyLocation():
if __name__ == "__main__":
runningFile = sys.argv[0] if os.path.isabs(sys.argv[0]) else
os.path.abs(sys.argv[0])
else:
runningFile = __file__
return runningFile

>

>C:/folder
Thanks,
Aine.

Aug 23 '07 #8

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

Similar topics

4
by: Barry G. Sumpter | last post by:
Complete newbie here. Just sorted out that myASP.asp will ONLY execute on c:\inetput\wwwroot when I access it thru http://localhost/myASP.asp but if I copy myASP.asp to a sub folder of...
0
by: Manjunatha | last post by:
I created an asp page that writes to cookies and copied it to a japanese virtual folder. The cookie writing code is below. first.asp var sUserId = new String(); var sPassword = new String();...
5
by: vishal | last post by:
Hi, I am writing my first script to execute a Oracle procedure through a unix script. Could you tell me how do I do error handling here? How do I know in my script if the procedure executed...
5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
6
by: Bart Schelkens | last post by:
Hi, how can I execute a piece of Java-script after a piece of vb-net-code has been executed? Thx
8
by: Paw | last post by:
Greetings. I use asp. what I need is is when a visitor comes to the site, I need it to check the host name. if "www.hometowndigest.com" is the host, then check a folder named "something" and if...
6
by: PipedreamerGrey | last post by:
I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file...
1
by: pnjbi | last post by:
Hi, I've created a bash script which is executed every 5 minutes through a cron. The script checks if a trigger variable has been set to execute certain commands. If it is the commands are...
3
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.