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

sys.argv[0] doesn't always contain the full path of running script.

Hi,

I use RedHat linux.

How can I find where exactly the current python script is running?

I use this code:

#test.py
import os,sys
print sys.argv
os.chdir(os.path.dirname(sys.argv[0]))
It doesn't work when I run this command from the directory that
test.py is located:

python test.py

That means sys.argv[0] doesn't always contain the full path of
running script.

Any help would be appreciated,
Max

Aug 29 '06 #1
7 5043
Hello Max,
How can I find where exactly the current python script is running?
...
That means sys.argv[0] doesn't always contain the full path of
running script.
sys.path[0] is the script directory, combined with sys.argv[0] you can
find the full path to the script.
(Note that in some rare cases sys.path[0] might not contain the script
directory. For example in an executable created by py2exe).

HTH.

--
Miki
http://pythonwise.blogspot.com

Aug 29 '06 #2
>How can I find where exactly the current python script is running?
>...
That means sys.argv[0] doesn't always contain the full path of
running script.
sys.path[0] is the script directory, combined with sys.argv[0] you can
find the full path to the script.
(Note that in some rare cases sys.path[0] might not contain the script
directory. For example in an executable created by py2exe).
I am not sure it is a good idea to rely on sys.path[0] being the current
directory.

I think the proper solution is to use: os.path.abspath(sys.argv[0])
If sys.argv[0] is a relative path, than adding cwd via the above function
will make it absolute as the gp wanted.

This may only break if the python program messes around with the cwd but in
that case its a good idea to extract os.path.abspath(sys.argv[0]) before
that.

Aug 29 '06 #3

gmax2006 wrote:
Hi,

I use RedHat linux.

How can I find where exactly the current python script is running?
Hi,

Doesnt __file__ attribute of each module contain the full filepath of
the module?
So, try this:

filepath = __file__
print filepath

Works for me :)

Cheers,
i. zuzak

Aug 30 '06 #4
>How can I find where exactly the current python script is running?
>
Doesnt __file__ attribute of each module contain the full filepath of
the module?
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:

my_script.py:
---------------------------
import my_module

---------------------------

my_module.py:
---------------------------
print __file__

---------------------------

Running "python test.py" now prints /path/to/my_module.py, not
/path/to/my_script.py.

Cheers!
/Joel Hedlund
Aug 31 '06 #5
Running "python test.py" now prints /path/to/my_module.py, not
/path/to/my_script.py.
That should have been "python my_script.py". Sorry for the slip-up.

Cheers!
/Joel
Aug 31 '06 #6

Joel Hedlund wrote:
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:
I thought that was the point - to get the full path of the running
script? I see you use the terms "script" and "module" in different
contexts, while I use them as: script = module = file. I can't say
which is right, though :).

Cheers,
ivan

Aug 31 '06 #7

Ivan Zuzak wrote:
Joel Hedlund wrote:
Yes indeed! But the path to the module will not be the same as the path to
the script if you are currently in an imported module. Consider this:

I thought that was the point - to get the full path of the running
script? I see you use the terms "script" and "module" in different
contexts, while I use them as: script = module = file. I can't say
which is right, though :).
If you execute fubar.py, it's a script with __name__ == "__main__"; if
you import it, it's a module __name__ == "fubar".

Aug 31 '06 #8

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

Similar topics

0
by: fowlertrainer | last post by:
Hi ! I have been finished my WMI information getter simple application, but the exe is not working as like the py modules before compilation. The problem that when I started the program from...
9
by: Daniel Schüle | last post by:
Hello I wrote a simple module, which is also supposed to be used as standalone program after considering how to avoid multiple if's I came up with this idea if __name__ == "__main__": if...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
2
by: johnny | last post by:
hi all, I wonder why this little script doesn't work, maybe it's the provider not allowing the use of load data infile ( I know some don't let users to run some tasks ), could you please tell me...
2
by: Thomas W | last post by:
I've created a simple script like so : import sys import wx app = wx.PySimpleApp() dlg = wx.MessageDialog(None, "%s" % sys.argv, 'A Message Box', wx.YES_NO | wx.ICON_QUESTION) retCode =...
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
2
by: tvjraman | last post by:
I've spent two days on google trying to find a suitable solution for getting the full path of the running process but in vain. My question is very simple. I want to know the full path of the...
2
by: Hai Vu | last post by:
I currently use ActivePython 2.5.1. Consider the following code which I saved as cmdline.py: import sys print sys.argv If I invoke this code as 'python cmdline.py', then the output is:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.