如何在python中更改目录(cd)?

How do I change directory (cd) in Python?

与shell命令中更改工作目录相同。

如何在python中更改当前工作目录?


您可以使用以下方式更改工作目录:

1
2
3
import os

os.chdir(path)

使用此方法时,需要遵循两个最佳实践:

  • 捕获无效路径上的异常(windowserror、oserror)。如果抛出异常,不要执行任何递归操作,尤其是破坏性的操作。他们将在旧的道路上行动,而不是新的道路上行动。
  • 完成后返回到旧目录。这可以用一种异常安全的方式来完成,方法是将chdir调用包装在上下文管理器中,就像BrianM.Hunt在他的答案中所做的那样。
  • 更改子进程中的当前工作目录不会更改父进程中的当前工作目录。这同样适用于Python解释器。您不能使用os.chdir()来更改调用进程的CWD。


    下面是一个更改工作目录的上下文管理器示例。它比其他地方提到的ActiveState版本简单,但这可以完成任务。

    上下文管理器:cd

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import os

    class cd:
       """Context manager for changing the current working directory"""
        def __init__(self, newPath):
            self.newPath = os.path.expanduser(newPath)

        def __enter__(self):
            self.savedPath = os.getcwd()
            os.chdir(self.newPath)

        def __exit__(self, etype, value, traceback):
            os.chdir(self.savedPath)

    或者使用ContextManager尝试更简洁的等价物(如下)。

    例子

    1
    2
    3
    4
    5
    6
    7
    8
    import subprocess # just to call an arbitrary command e.g. 'ls'

    # enter the directory like this:
    with cd("~/Library"):
       # we are in ~/Library
       subprocess.call("ls")

    # outside the context manager we are back wherever we started.


    我会这样使用os.chdir

    1
    os.chdir("/path/to/change/to")

    顺便说一下,如果您需要确定当前的路径,请使用os.getcwd()

    这里更多


    cd()使用生成器和修饰器很容易编写。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    from contextlib import contextmanager
    import os

    @contextmanager
    def cd(newdir):
        prevdir = os.getcwd()
        os.chdir(os.path.expanduser(newdir))
        try:
            yield
        finally:
            os.chdir(prevdir)

    然后,即使在引发异常之后,也会还原目录:

    1
    2
    3
    4
    5
    6
    os.chdir('/home')

    with cd('/tmp'):
        # ...
        raise Exception("There's no place like home.")
    # Directory is now back to '/home'.


    如果您使用的是相对较新版本的python,那么也可以使用上下文管理器,例如:

    1
    2
    3
    4
    5
    6
    7
    from __future__ import with_statement
    from grizzled.os import working_directory

    with working_directory(path_to_directory):
        # code in here occurs within the directory

    # code here is in the original directory

    更新

    如果你喜欢自己滚:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    import os
    from contextlib import contextmanager

    @contextmanager
    def working_directory(directory):
        owd = os.getcwd()
        try:
            os.chdir(directory)
            yield directory
        finally:
            os.chdir(owd)


    正如其他人已经指出的,上面的所有解决方案都只更改当前流程的工作目录。当您退出到unix shell时,这将丢失。如果你不顾一切的话,你可以用这个可怕的黑客程序更改unix上的父shell目录:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    def quote_against_shell_expansion(s):
        import pipes
        return pipes.quote(s)

    def put_text_back_into_terminal_input_buffer(text):
        # use of this means that it only works in an interactive session
        # (and if the user types while it runs they could insert characters between the characters in 'text'!)
        import fcntl, termios
        for c in text:
            fcntl.ioctl(1, termios.TIOCSTI, c)

    def change_parent_process_directory(dest):
        # the horror
        put_text_back_into_terminal_input_buffer("cd"+quote_against_shell_expansion(dest)+"
    "
    )


    os.chdir()是正确的方法。


    os.chdir()cd的肾盂版。


    进一步深入到布莱恩指出的方向,基于sh(1.0.8+)

    1
    2
    3
    4
    from sh import cd, ls

    cd('/tmp')
    print ls()

    如果要执行"cd…"选项,只需键入:

    O.CHDIR("…")

    它与Windows命令:cd中的相同。当然,导入操作系统是必需的(例如,将其键入代码的第一行)


    1
    2
    3
    4
    5
    6
    7
    import os

    abs_path = 'C://a/b/c'
    rel_path = './folder'

    os.chdir(abs_path)
    os.chdir(rel_path)

    您可以同时使用os.chdir(abs_path)或os.chdir(rel_path),不需要调用os.getcwd()来使用相对路径。


    1
    2
    3
    4
    5
    6
    7
    8
    #import package
    import os

    #change directory
    os.chdir('my_path')

    #get location
    os.getcwd()

    另外,最好在这里检查OS包中所有其他有用的命令:https://docs.python.org/3/library/os.html


    更改脚本进程的当前目录非常简单。我认为问题实际上是如何更改命令窗口中调用Python脚本的当前目录,这非常困难。Windows中的BAT脚本或bash shell中的bash脚本可以使用普通的cd命令来执行此操作,因为shell本身就是解释器。在Windows和Linux中,python都是一个程序,任何程序都不能直接更改其父环境。然而,将简单的shell脚本与执行大多数硬操作的python脚本结合在一起可以获得所需的结果。例如,为了使扩展CD命令具有向后/向前/选择重访的遍历历史,我编写了一个简单的BAT脚本调用的相对复杂的python脚本。遍历列表存储在一个文件中,目标目录位于第一行。当python脚本返回时,bat脚本读取文件的第一行,并将其作为cd的参数。完整的BAT脚本(为了简洁起见减去注释)是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    if _%1 == _. goto cdDone
    if _%1 == _? goto help
    if /i _%1 NEQ _-H goto doCd
    :help
    echo d.bat and dSup.py 2016.03.05. Extended chdir.
    echo -C = clear traversal list.
    echo -B or nothing = backward (to previous dir).
    echo -F or - = forward (to next dir).
    echo -R = remove current from list and return to previous.
    echo -S = select from list.
    echo -H, -h, ? = help.
    echo . = make window title current directory.
    echo Anything else = target directory.
    goto done

    :doCd
    %~dp0dSup.py %1
    for /F %%d in ( %~dp0dSupList ) do (
        cd %%d
        if errorlevel 1 ( %~dp0dSup.py -R )
        goto cdDone
    )
    :cdDone
    title %CD%
    :done

    python脚本dsup.py是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    import sys, os, msvcrt

    def indexNoCase ( slist, s ) :
        for idx in range( len( slist )) :
            if slist[idx].upper() == s.upper() :
                return idx
        raise ValueError

    # .........main process ...................
    if len( sys.argv ) < 2 :
        cmd = 1 # No argument defaults to -B, the most common operation
    elif sys.argv[1][0] == '-':
        if len(sys.argv[1]) == 1 :
            cmd = 2 # '-' alone defaults to -F, second most common operation.
        else :
            cmd = 'CBFRS'.find( sys.argv[1][1:2].upper())
    else :
        cmd = -1
        dir = os.path.abspath( sys.argv[1] ) + '
    '


    # cmd is -1 = path, 0 = C, 1 = B, 2 = F, 3 = R, 4 = S

    fo = open( os.path.dirname( sys.argv[0] ) + '\\dSupList', mode = 'a+t' )
    fo.seek( 0 )
    dlist = fo.readlines( -1 )
    if len( dlist ) == 0 :
        dlist.append( os.getcwd() + '
    '
    ) # Prime new directory list with current.

    if cmd == 1 : # B: move backward, i.e. to previous
        target = dlist.pop(0)
        dlist.append( target )
    elif cmd == 2 : # F: move forward, i.e. to next
        target = dlist.pop( len( dlist ) - 1 )
        dlist.insert( 0, target )
    elif cmd == 3 : # R: remove current from list. This forces cd to previous, a
                    # desireable side-effect
        dlist.pop( 0 )
    elif cmd == 4 : # S: select from list
    # The current directory (dlist[0]) is included essentially as ESC.
        for idx in range( len( dlist )) :
            print( '(' + str( idx ) + ')', dlist[ idx ][:-1])
        while True :
            inp = msvcrt.getche()
            if inp.isdigit() :
                inp = int( inp )
                if inp < len( dlist ) :
                    print( '' ) # Print the newline we didn't get from getche.
                    break
            print( ' is out of range' )
    # Select 0 means the current directory and the list is not changed. Otherwise
    # the selected directory is moved to the top of the list. This can be done by
    # either rotating the whole list until the selection is at the head or pop it
    # and insert it to 0. It isn't obvious which would be better for the user but
    # since pop-insert is simpler, it is used.
        if inp > 0 :
            dlist.insert( 0, dlist.pop( inp ))

    elif cmd == -1 : # -1: dir is the requested new directory.
    # If it is already in the list then remove it before inserting it at the head.
    # This takes care of both the common case of it having been recently visited
    # and the less common case of user mistakenly requesting current, in which
    # case it is already at the head. Deleting and putting it back is a trivial
    # inefficiency.
        try:
            dlist.pop( indexNoCase( dlist, dir ))
        except ValueError :
            pass
        dlist = dlist[:9] # Control list length by removing older dirs (should be
                          # no more than one).
        dlist.insert( 0, dir )

    fo.truncate( 0 )
    if cmd != 0 : # C: clear the list
        fo.writelines( dlist )

    fo.close()
    exit(0)


    为了便于交互使用,IPython内置了所有常见的shell命令。