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


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

what's the command for (cd ..) in python

Started bykaustubh joshi <kandrjoshi@gmail.com>
First post2011-09-09 14:07 +0200
Last post2011-09-11 09:50 +1000
Articles 7 — 5 participants

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


Contents

  what's the command for (cd ..) in python kaustubh joshi <kandrjoshi@gmail.com> - 2011-09-09 14:07 +0200
    Re: what's the command for (cd ..) in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-09 23:03 +1000
      Re: what's the command for (cd ..) in python Terry Reedy <tjreedy@udel.edu> - 2011-09-09 13:35 -0400
      Re: what's the command for (cd ..) in python "Waldek M." <wm@localhost.localdomain> - 2011-09-10 12:32 +0200
        Re: what's the command for (cd ..) in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-10 21:11 +1000
          Re: what's the command for (cd ..) in python "Waldek M." <wm@localhost.localdomain> - 2011-09-10 15:57 +0200
            Re: what's the command for (cd ..) in python Cameron Simpson <cs@zip.com.au> - 2011-09-11 09:50 +1000

#13020 — what's the command for (cd ..) in python

Fromkaustubh joshi <kandrjoshi@gmail.com>
Date2011-09-09 14:07 +0200
Subjectwhat's the command for (cd ..) in python
Message-ID<mailman.903.1315570033.27778.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello friends,
                         How do we carry out the command "*cd ..*" in
python?

My problem is :
I have a set of folders say m=1,2,3,4. In each of these folders, I have
subfolders with common name say m_5,m_6,m_7,m_8. In each of these subfolder,
there is a file which I have edit.
                                          1
         2                                    3
       4
                                         |
          |                                      |
            |

------------------------------------------------------------------------------------------------------------------------------------------
                                  1_5, 1_6, 1_7, 1_8               2_5 ,2_6,
2_7, 2_8           3_5, 3_6, 3_7, 3_8          4_5, 4_6, 4_7, 4_8

That is how I designed it

When I run my script, it follows the route 1 ---> 1_5-----> do the edit job
in the file. Now it need to change the  subfolder from 1_5 to 1_6, which is
not happening.

I created the folders using variable like m for folder taking values 1,2,3,4
and m_n for subfolders with n=5,6,7,8.

I am trying with os.chdir(path), but stuck with how to use m_n in it.

What I am missing at the moment is something that do the job "cd .." does.

Any help


Karan

[toc] | [next] | [standalone]


#13023

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-09-09 23:03 +1000
Message-ID<4e6a0e8e$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#13020
kaustubh joshi wrote:

> Hello friends,
>                          How do we carry out the command "*cd ..*" in
> python?

import os
os.chdir('..')

But think carefully before doing this. Some functions may be confused if you
change directories while they are running. You may be better off staying in
the same directory, and adjusting the path names to the files as you work
with them.



-- 
Steven

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


#13032

FromTerry Reedy <tjreedy@udel.edu>
Date2011-09-09 13:35 -0400
Message-ID<mailman.911.1315590009.27778.python-list@python.org>
In reply to#13023
On 9/9/2011 9:03 AM, Steven D'Aprano wrote:
> kaustubh joshi wrote:
>
>> Hello friends,
>>                           How do we carry out the command "*cd ..*" in
>> python?
>
> import os
> os.chdir('..')
>
> But think carefully before doing this. Some functions may be confused if you
> change directories while they are running. You may be better off staying in
> the same directory, and adjusting the path names to the files as you work
> with them.

Or, use it once at the top of the main module, with an absolute path, to 
make sure you start at a known place where you want to start.

-- 
Terry Jan Reedy

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


#13062

From"Waldek M." <wm@localhost.localdomain>
Date2011-09-10 12:32 +0200
Message-ID<eeimd1h6ntq0$.dlg@localhost.localdomain>
In reply to#13023
On Fri, 09 Sep 2011 23:03:10 +1000, Steven D'Aprano wrote:
> But think carefully before doing this. Some functions may be confused if you
> change directories while they are running. You may be better off staying in
> the same directory, and adjusting the path names to the files as you work
> with them.

Curious.
Do you mean multi-threaded environment or even in single thread?
If the latter is the case, I'd say those functions make 
very nasty assumptions. Who are they, anyways? ;)

Br.
Waldek

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


#13063

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-09-10 21:11 +1000
Message-ID<4e6b45e6$0$29970$c3e8da3$5496439d@news.astraweb.com>
In reply to#13062
Waldek M. wrote:

> On Fri, 09 Sep 2011 23:03:10 +1000, Steven D'Aprano wrote:
>> But think carefully before doing this. Some functions may be confused if
>> you change directories while they are running. You may be better off
>> staying in the same directory, and adjusting the path names to the files
>> as you work with them.
> 
> Curious.
> Do you mean multi-threaded environment or even in single thread?
> If the latter is the case, I'd say those functions make
> very nasty assumptions. Who are they, anyways? ;)

The main one that comes to mind is os.walk, which has this to say:

    Caution:  if you pass a relative pathname for top, don't change the
    current working directory between resumptions of walk.  walk never
    changes the current directory, and assumes that the client doesn't
    either.

Seems like a reasonable assumption to me.

-- 
Steven

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


#13070

From"Waldek M." <wm@localhost.localdomain>
Date2011-09-10 15:57 +0200
Message-ID<5yll66t4xv3j.dlg@localhost.localdomain>
In reply to#13063
On Sat, 10 Sep 2011 21:11:32 +1000, Steven D'Aprano wrote:
> The main one that comes to mind is os.walk, which has this to say:
> 
>     Caution:  if you pass a relative pathname for top, don't change the
>     current working directory between resumptions of walk.  walk never
>     changes the current directory, and assumes that the client doesn't
>     either.
> 
> Seems like a reasonable assumption to me.

Oh, that kind of functions.
Yes, that *is* surely reasonable, just as mixing chdir/mkdir/remove
and whatever else that affects the directory structure is not a good idea,
or at least something to be done carefully.
I just wouldn't think to give it some special credit.

Thanks
Waldek

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


#13091

FromCameron Simpson <cs@zip.com.au>
Date2011-09-11 09:50 +1000
Message-ID<mailman.957.1315698651.27778.python-list@python.org>
In reply to#13070
On 10Sep2011 15:57, Waldek M. <wm@localhost.localdomain> wrote:
| On Sat, 10 Sep 2011 21:11:32 +1000, Steven D'Aprano wrote:
| > The main one that comes to mind is os.walk, which has this to say:
| > 
| >     Caution:  if you pass a relative pathname for top, don't change the
| >     current working directory between resumptions of walk.  walk never
| >     changes the current directory, and assumes that the client doesn't
| >     either.
| > 
| > Seems like a reasonable assumption to me.
| 
| Oh, that kind of functions.
| Yes, that *is* surely reasonable, just as mixing chdir/mkdir/remove
| and whatever else that affects the directory structure is not a good idea,
| or at least something to be done carefully.
| I just wouldn't think to give it some special credit.

It's like umask and friends - they affect a program-wide implicit state.
Umask affects default new file permissions, chdir affects starting point
for relative pathnames, etc. All implicit.

The usual biggie with chdir is that of course your program may have been
handed relative file pathnames on the command line. They need resolving
_before_ any chdiring happens, if chdiring is going to happen.

For this kind of reason chdir, umask et al are generally best used in
top level logic only rather than inside library functions.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It's hard to believe that something as rational as the metric system was
invented by the French. - Henry O. Farad <lrc@netcom.COM>

[toc] | [prev] | [standalone]


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


csiph-web