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


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

Why doesn't Python remember the initial directory?

Started bykj <no.email@please.post>
First post2012-08-19 20:42 +0000
Last post2012-08-19 17:54 -0700
Articles 8 on this page of 28 — 18 participants

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


Contents

  Why doesn't Python remember the initial directory? kj <no.email@please.post> - 2012-08-19 20:42 +0000
    Re: Why doesn't Python remember the initial directory? Giacomo Alzetta <giacomo.alzetta@gmail.com> - 2012-08-19 14:01 -0700
      Re: Why doesn't Python remember the initial directory? Nobody <nobody@nowhere.com> - 2012-08-20 01:45 +0100
        Re: Why doesn't Python remember the initial directory? Neil Hodgson <nhodgson@iinet.net.au> - 2012-08-21 11:39 +1000
    Re: Why doesn't Python remember the initial directory? Roy Smith <roy@panix.com> - 2012-08-19 17:03 -0400
      Re: Why doesn't Python remember the initial directory? kj <no.email@please.post> - 2012-08-20 01:57 +0000
        Re: Why doesn't Python remember the initial directory? Jerry Hill <malaclypse2@gmail.com> - 2012-08-19 22:05 -0400
        Re: Why doesn't Python remember the initial directory? alex23 <wuwei23@gmail.com> - 2012-08-19 19:57 -0700
          Re: Why doesn't Python remember the initial directory? alex23 <wuwei23@gmail.com> - 2012-08-19 20:04 -0700
            Re: Why doesn't Python remember the initial directory? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-20 08:11 +0100
          Re: Why doesn't Python remember the initial directory? alex23 <wuwei23@gmail.com> - 2012-08-19 20:03 -0700
          Re: Why doesn't Python remember the initial directory? alex23 <wuwei23@gmail.com> - 2012-08-19 19:59 -0700
          Re: Why doesn't Python remember the initial directory? Paul Rubin <no.email@nospam.invalid> - 2012-08-20 21:20 -0700
        Re: Why doesn't Python remember the initial directory? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-20 03:38 +0000
          Re: Why doesn't Python remember the initial directory? alex23 <wuwei23@gmail.com> - 2012-08-19 20:43 -0700
        Re: Why doesn't Python remember the initial directory? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-20 08:10 +0100
        Re: Why doesn't Python remember the initial directory? andrea crotti <andrea.crotti.0@gmail.com> - 2012-08-20 12:56 +0100
          Re: Why doesn't Python remember the initial directory? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-20 13:14 +0000
            Re: Why doesn't Python remember the initial directory? Walter Hurry <walterhurry@lavabit.com> - 2012-08-20 13:49 +0000
              Re: Why doesn't Python remember the initial directory? Roy Smith <roy@panix.com> - 2012-08-20 09:58 -0400
                Re: Why doesn't Python remember the initial directory? andrea crotti <andrea.crotti.0@gmail.com> - 2012-08-20 15:15 +0100
        Re: Why doesn't Python remember the initial directory? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-20 16:39 +0200
        Re: Why doesn't Python remember the initial directory? Grant Edwards <invalid@invalid.invalid> - 2012-08-20 16:28 +0000
        Re: Why doesn't Python remember the initial directory? Piet van Oostrum <piet@vanoostrum.org> - 2012-08-20 16:06 -0400
        Re: Why doesn't Python remember the initial directory? John Roth <johnroth1@gmail.com> - 2012-08-21 13:35 -0700
    Re: Why doesn't Python remember the initial directory? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-19 22:18 +0100
    Re: Why doesn't Python remember the initial directory? Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-19 23:05 +0200
    Re: Why doesn't Python remember the initial directory? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-19 17:54 -0700

Page 2 of 2 — ← Prev page 1 [2]


#27495

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2012-08-20 15:15 +0100
Message-ID<mailman.3556.1345472139.4697.python-list@python.org>
In reply to#27494
2012/8/20 Roy Smith <roy@panix.com>:
> In article <k0tf8g$adc$1@news.albasani.net>,
>  Walter Hurry <walterhurry@lavabit.com> wrote:
>
>> It is difficult to think of a sensible use for os.chdir, IMHO.
>
> It is true that you can mostly avoid chdir() by building absolute
> pathnames, but it's often more convenient to just cd somewhere and use
> names relative to that.  Fabric (a very cool tool for writing remote
> sysadmin scripts), gives you a cd() command which is a context manager,
> making it extra convenient.
>
> Also, core files get created in the current directory.  Sometimes
> daemons will cd to some fixed location to make sure that if they dump
> core, it goes in the right place.
>
> On occasion, you run into (poorly designed, IMHO) utilities which insist
> of reading or writing a file in the current directory.  If you're
> invoking one of those, you may have no choice but to chdir() to the
> right place before running them.
> --
> http://mail.python.org/mailman/listinfo/python-list


I've done quite a lot of system programming as well, and changing
directory is only a source of possible troubles in general.

If I really have to for some reasons I do this


class TempCd:
    """Change temporarily the current directory
    """
    def __init__(self, newcwd):
        self.newcwd = newcwd
        self.oldcwd = getcwd()

    def __enter__(self):
        chdir(self.newcwd)
        return self

    def __exit__(self, type, value, traceback):
        chdir(self.oldcwd)


with TempCd('/tmp'):
    # now working in /tmp

# now in the original

So it's not that hard to avoid problems..

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


#27497

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-08-20 16:39 +0200
Message-ID<mailman.3558.1345473594.4697.python-list@python.org>
In reply to#27449
kj wrote:
> 99.99% of Python programmers
> will find that there's nothing wrong with behavior 
[snip]
> Pardon my cynicism, but the general vibe from the replies I've
> gotten to my post (i.e. "if Python ain't got it, it means you don't
> need it") 
>   
[snip]

Don't you find there's something wrong in applying your observation from 
2 or 3 replies to your post to 99% of python programmer ? Moreover, 
flaming people on their own mailing list won't do you any good, ever, in 
any list.

To get back to your original question,

 > inspect.getmodule?
Docstring:
    Return the module an object was defined in, or None if not found.

As getmodule may return None if not found, you need too handle that 
case. There's possibly a weakness in the inspect module when changing 
the current directory however nothing wrong with Python having the 
remember the intial directory. If you need to remember it, do it youself 
(or file a bug to inspect module authors).

JM

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


#27507

FromGrant Edwards <invalid@invalid.invalid>
Date2012-08-20 16:28 +0000
Message-ID<k0tokb$arb$1@reader1.panix.com>
In reply to#27449
On 2012-08-20, kj <no.email@please.post> wrote:
> In <roy-CA6D77.17031119082012@news.panix.com> Roy Smith <roy@panix.com> writes:
>
>>In article <k0rj38$2gc$1@reader1.panix.com>, kj <no.email@please.post> 
>>wrote:
>
>>> As far as I've been able to determine, Python does not remember
>>> (immutably, that is) the working directory at the program's start-up,
>>> or, if it does, it does not officially expose this information.
>
>>Why would you expect that it would?  What would it (or you) do with this 
>>information?
>
> This means that no library code can ever count on, for example,
> being able to reliably find the path to the file that contains the
> definition of __main__.

What makes you think that the file that contains the definition of
__main__ is the working directory on program startup?  That's almost
never the case in my experience.

2) Why should a library expect to be able to access the file
   containing the definition of __main__.  

> That's a weakness, IMO.

You must be in possession of some rather odd use cases.  I've been
writing Python programs for something like 13 years, and I've never
felt any need to have either an immutable record of the startup
directory or access to the file containing the definition of __main__.

> I don't know of any way to fix inspect.getmodule that does not
> involve, directly or indirectly, keeping a stable record of the
> starting directory.

If what you really want is access to the definition of __main__, what
does that have to do with the startup directory?

If you want to know where __main__ is, you can probably figure it out
from /proc/self/<something-or-other>


-- 
Grant Edwards               grant.b.edwards        Yow! Am I having fun yet?
                                  at               
                              gmail.com            

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


#27529

FromPiet van Oostrum <piet@vanoostrum.org>
Date2012-08-20 16:06 -0400
Message-ID<m2wr0tl452.fsf@cochabamba.vanoostrum.org>
In reply to#27449
kj <no.email@please.post> writes:

> This means that no library code can ever count on, for example,
> being able to reliably find the path to the file that contains the
> definition of __main__.  That's a weakness, IMO.  

On Unix based systems there is no reliable way to find out this
information. So how could Python reliably supply this?
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

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


#27592

FromJohn Roth <johnroth1@gmail.com>
Date2012-08-21 13:35 -0700
Message-ID<ca5b0ab8-740c-4139-b535-66919a8cfbaa@googlegroups.com>
In reply to#27449
On Sunday, August 19, 2012 7:57:46 PM UTC-6, kj wrote:

> This means that no library code can ever count on, for example,
> 
> being able to reliably find the path to the file that contains the
> 
> definition of __main__. 

If you want to find that, look up __main__ in sys.modules and then look at the __file__ attribute. You need to be a bit careful with this; the import machinery was rewritten for the upcoming 3.3 release, and there were several changes to the module information.

John Roth

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


#27433

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-08-19 22:18 +0100
Message-ID<mailman.3526.1345411063.4697.python-list@python.org>
In reply to#27429
On 19/08/2012 21:42, kj wrote:
>
>
> As far as I've been able to determine, Python does not remember
> (immutably, that is) the working directory at the program's start-up,
> or, if it does, it does not officially expose this information.
>
> Does anyone know why this is?  Is there a PEP stating the rationale
> for it?
>
> Thanks!
>

Why would you have a Python Enhancement Proposal to state the rationale 
for this?

-- 
Cheers.

Mark Lawrence.

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


#27434

FromLaszlo Nagy <gandalf@shopzeus.com>
Date2012-08-19 23:05 +0200
Message-ID<mailman.3527.1345411526.4697.python-list@python.org>
In reply to#27429
On 2012-08-19 22:42, kj wrote:
>
> As far as I've been able to determine, Python does not remember
> (immutably, that is) the working directory at the program's start-up,
> or, if it does, it does not officially expose this information.
>
> Does anyone know why this is?  Is there a PEP stating the rationale
> for it?
>
> Thanks!
When you start the program, you have a current directory. When you 
change it, then it is changed. How do you want Python to remember a 
directory? For example, you can put it into a variable, and use it 
later. Can you please show us some example code that demonstrates your 
actual problem?

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


#27447

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-08-19 17:54 -0700
Message-ID<c0581607-a066-4a1a-93c4-0a4ba9dc1229@googlegroups.com>
In reply to#27429
On Monday, August 20, 2012 4:42:16 AM UTC+8, kj wrote:
> As far as I've been able to determine, Python does not remember
> 
> (immutably, that is) the working directory at the program's start-up,
> 
> or, if it does, it does not officially expose this information.
> 
> 
> 
> Does anyone know why this is?  Is there a PEP stating the rationale
> 
> for it?
> 
> 
> 
> Thanks!

Immutable data can be frozen and saved in somewhere off the main memory.

Perative and imperative programming are different.

Please check Erlang.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web