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. 7 1085
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/
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
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/
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
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.
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
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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();...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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!
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |