相当于shell'cd'命令来更改工作目录?

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

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

1354706 次浏览

os.chdir()cd的Pythonic版本。

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

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

import os
os.chdir(path)

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

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

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

我将像这样使用os.chdir

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

顺便说一句,如果您需要找出当前的路径,请使用os.getcwd()

更多这里

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

from __future__ import with_statementfrom 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 osfrom contextlib import contextmanager
@contextmanagerdef working_directory(directory):owd = os.getcwd()try:os.chdir(directory)yield directoryfinally:os.chdir(owd)

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

上下文管理器: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)

或者尝试更简洁的等效(下),使用上下文管理器

示例

import subprocess # just to call an arbitrary command e.g. 'ls'
# enter the directory like this:with cd("~/Library"):# we are in ~/Librarysubprocess.call("ls")
# outside the context manager we are back wherever we started.

正如其他人已经指出的那样,上面的所有解决方案只会更改当前进程的工作目录。当你退出回到Unix shell时,这会丢失。如果绝望,你可以使用这个可怕的黑客更改Unix上的父shell目录:

def quote_against_shell_expansion(s):import pipesreturn 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, termiosfor c in text:fcntl.ioctl(1, termios.TIOCSTI, c)
def change_parent_process_directory(dest):# the horrorput_text_back_into_terminal_input_buffer("cd "+quote_against_shell_expansion(dest)+"\n")

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

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

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

from contextlib import contextmanagerimport os
@contextmanagerdef cd(newdir):prevdir = os.getcwd()os.chdir(os.path.expanduser(newdir))try:yieldfinally:os.chdir(prevdir)

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

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

更改脚本进程的当前目录是微不足道的。我认为问题实际上是如何更改调用python脚本的命令窗口的当前目录,这是非常困难的。Windows中的Bat脚本或Bash shell中的Bash脚本可以用普通的cd命令做到这一点,因为shell本身就是解释器。在Windows和LinuxPython都是一个程序,没有程序可以直接更改其父级的环境。然而,将简单的外壳脚本与Python脚本相结合可以实现预期的结果。例如,为了使扩展的cd命令具有向后/向前/选择重访的遍历历史记录,我编写了一个相对复杂的Python脚本,由一个简单的bat脚本调用。遍历列表存储在一个文件中,第一行是目标目录。当python脚本返回时,bat脚本读取文件的第一行并将其作为cd的参数。完整的bat脚本(为简洁起见减去注释)是:

if _%1 == _. goto cdDoneif _%1 == _? goto helpif /i _%1 NEQ _-H goto doCd:helpecho 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 %1for /F %%d in ( %~dp0dSupList ) do (cd %%dif errorlevel 1 ( %~dp0dSup.py -R )goto cdDone):cdDonetitle %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 idxraise ValueError
# .........main process ...................if len( sys.argv ) < 2 :cmd = 1 # No argument defaults to -B, the most common operationelif 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 = -1dir = 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 previoustarget = dlist.pop(0)dlist.append( target )elif cmd == 2 : # F: move forward, i.e. to nexttarget = 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-effectdlist.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.breakprint( ' 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 :passdlist = 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 listfo.writelines( dlist )
fo.close()exit(0)

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

os.chdir(“…”)

它与Windows中的cmd: cd相同。当然导入操作系统是必要的(例如将其输入为代码的第一行)

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()来使用相对路径。

如果您使用spyder并且喜欢GUI,您可以简单地单击屏幕右上角的文件夹按钮并在您想要作为当前目录的文件夹/目录中导航。完成后,您可以转到spyder IDE中窗口的文件资源管理器选项卡,您可以看到那里存在的所有文件/文件夹。检查您当前的工作目录转到spyder IDE的控制台,只需键入

pwd

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

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

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