【问题标题】:Equivalent of shell 'cd' command to change the working directory?等效于 shell 'cd' 命令来更改工作目录?
【发布时间】:2010-09-30 16:59:56
【问题描述】:

cd是改变工作目录的shell命令。

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

【问题讨论】:

标签: python cd


【解决方案1】:

您可以使用以下命令更改工作目录:

import os

os.chdir(path)

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

  1. 在无效路径上捕获异常(WindowsError、OSError)。如果抛出异常,请不要执行任何递归操作,尤其是破坏性操作。他们将在旧路径上运行,而不是在新路径上运行。
  2. 完成后返回旧目录。这可以通过将 chdir 调用包装在上下文管理器中以异常安全的方式完成,就像 Brian M. Hunt 在 his answer 中所做的那样。

更改子进程中的当前工作目录不会更改父进程中的当前工作目录。 Python 解释器也是如此。你不能使用os.chdir()来改变调用进程的CWD。​​p>

【讨论】:

  • cdunn2001 的轻量级decorator-based answer 是现代 Python 的理想方法。上面的答案说明了原因。 切勿在上下文管理器之外调用 os.chdir() 除非您认为自己知道自己在做什么。 (你可能不知道。
  • 我认为这是交互式 shell 中最简单的方法。请注意,在 Windows 中,您必须使用正斜杠,例如 os.chdir("C:/path/to/location")
  • 要注意的一件事是,如果您将 python 程序设置为可执行文件并在 cron 中运行,它将在您的主目录中启动。所以最好使用完全合格的路径。这绝对有效,但我仍然在我可能从 Python 调用的任何脚本中使用完全限定路径,因为不能保证这将适用于 Python 程序本身之外。
  • 在 Windows 中,如果使用反斜杠复制路径,则使用原始字符串会更容易。 r'C:\path\to\location'.
【解决方案2】:

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

上下文管理器:cd

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)

或者尝试more concise equivalent(below),使用ContextManager

示例

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.

【讨论】:

  • 如果你想知道你从哪个目录改变了,你可以在__enter__的末尾添加return self。这样你就可以做with cd('foo') as cm: 并以cm.savedPath 访问以前的目录@
  • 请注意,在某些情况下,无法返回旧目录(存储在“savedPath”中的目录)。例如,如果一个特权较高的进程运行一个特权较低的进程,则第二个进程会继承第一个进程的工作目录,即使在第二个进程无法以自己的能力进入该工作目录的情况下也是如此。
  • 我收到以下警告消息:Attribute 'savedPath' defined outside __init__ [attribute-defined-outside-init]
  • 如果我在with cd("~/Library"): 块内return 它仍然有效吗?
【解决方案3】:

我会像这样使用os.chdir

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

顺便说一句,如果你需要弄清楚你当前的路径,请使用os.getcwd()

更多here

【讨论】:

    【解决方案4】:

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

    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)
    

    然后,即使抛出异常,目录也会恢复:

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

    【讨论】:

    • 另外,请注意this potential blunder(忘记try/finally)。
    • Brilliance! 如果将来自accepted answer 的介绍性评论注入到this 答案中,这将是非常理想的。尽管如此,这个答案简洁,Python 安全的实现保证了我必须给予的所有支持。
    • 为什么是yield 而不是return?这应该是发电机吗?
    • @NicoBerrogorry,它是一个生成器。请参阅contextlib.contextmanager 上的文档。这是 Python 中非常有用的模式,值得学习。
    • @AndoJurai 我实际上认为,总是回到上一个目录是有意的。这样你的代码结构就和目录结构匹配了,就不用放cd('../')了,很容易忘记。
    【解决方案5】:

    如果您使用的是相对较新的 Python 版本,还可以使用上下文管理器,例如 this one

    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
    

    更新

    如果你喜欢自己动手:

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

    【讨论】:

    【解决方案6】:

    正如其他人已经指出的那样,上述所有解决方案仅更改当前进程的工作目录。当您退出到 Unix shell 时,这将丢失。如果绝望,你可以用这个可怕的 hack 改变 Unix 上的父 shell 目录:

    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)+"\n")
    

    【讨论】:

    • 疯狂、脆弱的黑客获得强制投票。 任何人都不应该这样做,尤其是“如果用户在运行时键入......”警告。尽管如此,看到改变父 CWD 有点但实际上并不可行,这让我内心的反叛者感到兴奋。点赞!为所有人点赞!
    【解决方案7】:

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

    【讨论】:

      【解决方案8】:

      os.chdir()cd 的 Pythonic 版本。

      【讨论】:

        【解决方案9】:
        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() 来使用相对路径。

        【讨论】:

        • 效果很好。可以使用 os.getcwd() 在更改目录之前和之后验证当前目录..
        【解决方案10】:

        进一步了解 Brian 指出的方向并基于 sh (1.0.8+)

        from sh import cd, ls
        
        cd('/tmp')
        print ls()
        

        【讨论】:

          【解决方案11】:

          如果您想执行类似“cd..”选项的操作,只需键入:

          os.chdir("..")

          它与 Windows cmd 中的相同:cd.. 当然 import os 是必需的(例如,将其作为代码的第一行输入)

          【讨论】:

            【解决方案12】:

            path 库中的 Path 对象为此提供了上下文管理器和 chdir 方法:

            from path import Path
            
            with Path("somewhere"):
                ...
            
            Path("somewhere").chdir()
            

            【讨论】:

              【解决方案13】:

              如果您使用 spyder 和 love GUI,您只需单击屏幕右上角的文件夹按钮,然后浏览您想要作为当前目录的文件夹/目录。 这样做之后,您可以转到 spyder IDE 窗口的文件资源管理器选项卡,您可以看到那里存在的所有文件/文件夹。 检查您当前的工作目录 转到 spyder IDE 的控制台,只需键入

              pwd
              

              它将打印您之前选择的相同路径。

              【讨论】:

                【解决方案14】:

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

                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是:

                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] ) + '\n'
                
                # 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() + '\n' ) # 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)
                

                【讨论】:

                • 虽然这是一个不错的答案,但 OP 选择了一个答案,表明它与更改父进程的 CWD 无关。这消除了对问题含义的任何可能的混淆。
                • 致铁皮人——在我发布我的建议之前选择了这个答案。我认为广泛的答案可能令人困惑。 cd 在给定进程(即 python 脚本)中是如此简单,以至于我不知道为什么有人会问它。
                • 实际上该答案是在 前选择的。如果它不合适,从那时起它会被多次调用。
                • 我认为混乱仍然存在。最近,问题“在 python 中模拟 linux 的“cd”命令,并在程序退出后保持目录更改 [重复]”被驳回,因为已在此处回答,但实际上,所选答案并未解决此问题。我的建议是针对 Windows,但 Linux 中的问题是一样的。
                猜你喜欢
                • 2020-02-26
                • 2021-09-10
                • 2018-10-03
                • 2015-01-01
                • 1970-01-01
                • 2011-10-12
                • 2016-12-05
                相关资源
                最近更新 更多