Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17143 > unrolled thread

Getting the current directory path and the path where python script is saved

Started bygialloporpora <gialloporpora@gmail.com>
First post2011-12-13 17:04 +0100
Last post2011-12-13 17:38 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Getting the current directory path and the path where python script is saved gialloporpora <gialloporpora@gmail.com> - 2011-12-13 17:04 +0100
    Re: Getting the current directory path and the path where python script is saved Neil Cerutti <neilc@norwich.edu> - 2011-12-13 16:29 +0000
      Re: Getting the current directory path and the path where python script is saved gialloporpora <gialloporpora@gmail.com> - 2011-12-13 22:39 +0100
    Re: Getting the current directory path and the path where python script is saved Thomas Bach <bachth@uni-mainz.de> - 2011-12-13 17:38 +0100

#17143 — Getting the current directory path and the path where python script is saved

Fromgialloporpora <gialloporpora@gmail.com>
Date2011-12-13 17:04 +0100
SubjectGetting the current directory path and the path where python script is saved
Message-ID<jc7t7n$u95$1@tdi.cu.mi.it>
Dear all,
I would like to create a little script to rename my files, I think to 
use argparse module to create it.
I would like to know how to obtain the path where script is executed and 
the path where the file script is stored.

For example, if I save my script in c:\python27 and I put the directory 
in %path% I can execute it from any folder, I would like to get the two 
directory path.

Thanks
Sandro

-- 
*gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu

Do it now. You will not regret it. #nyan

/via @beaulebens * - http://bit.ly/u2boVC
Sto ascoltando: *Pink Floyd – Echoe Part I * - http://bit.ly/vgIGQe

[toc] | [next] | [standalone]


#17149

FromNeil Cerutti <neilc@norwich.edu>
Date2011-12-13 16:29 +0000
Message-ID<9kpcqkFcoU1@mid.individual.net>
In reply to#17143
On 2011-12-13, gialloporpora <gialloporpora@gmail.com> wrote:
> Dear all,
> I would like to create a little script to rename my files, I think to 
> use argparse module to create it.
> I would like to know how to obtain the path where script is executed and 
> the path where the file script is stored.
>
> For example, if I save my script in c:\python27 and I put the
> directory in %path% I can execute it from any folder, I would
> like to get the two directory path.

Current working directory is available with os.getcwd()

The location of the script you are running may be available as
os.path.basename(sys.argv[0]).

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#17173

Fromgialloporpora <gialloporpora@gmail.com>
Date2011-12-13 22:39 +0100
Message-ID<jc8gsi$f6t$1@tdi.cu.mi.it>
In reply to#17149
Risposta al messaggio di Neil Cerutti :

> Current working directory is available with os.getcwd()
>
> The location of the script you are running may be available as
> os.path.basename(sys.argv[0]).
Thank you very much,  Neil, is exactly what I wanted.
Sandro


-- 
*gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu

Do it now. You will not regret it. #nyan

/via @beaulebens * - http://bit.ly/u2boVC
Sto ascoltando: *Pink Floyd – Echoe Part I * - http://bit.ly/vgIGQe

[toc] | [prev] | [next] | [standalone]


#17150

FromThomas Bach <bachth@uni-mainz.de>
Date2011-12-13 17:38 +0100
Message-ID<87y5ug2zrh.fsf@jubold.box>
In reply to#17143
gialloporpora <gialloporpora@gmail.com> writes:

> I would like to know how to obtain the path where script is executed
> and the path where the file script is stored.

Does this help:

In [16]: import os

In [19]: os.getcwd()
Out[19]: '/home/vince'

In [21]: os.__file__
Out[21]: '/home/vince/src/pyramid_install/lib/python2.7/os.pyc'

regards

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web