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

2024-11-29 08:41:00
admin
原创
140
摘要:问题描述:cd是用于改变工作目录的 shell 命令。如何在 Python 中更改当前工作目录?解决方案 1:您可以使用以下方法更改工作目录:import os os.chdir(path) 您应该注意,更改目录可能会导致您的代码在新位置应用破坏性更改。更糟糕的是,在更改目录后不要捕获诸如WindowsEr...

问题描述:

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

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


解决方案 1:

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

import os

os.chdir(path)

您应该注意,更改目录可能会导致您的代码在新位置应用破坏性更改。更糟糕的是,在更改目录后不要捕获诸如WindowsError和 之类的异常,因为这可能意味着在OSError位置应用了破坏性更改!

如果您使用的是 Python 3.11 或更新版本,请考虑使用此上下文管理器来确保您在完成后返回到原始工作目录:

from contextlib import chdir

with chdir(path):
    # do stuff here

如果您使用的是旧版本的 Python,Brian M. Hunt 的回答将向您展示如何推出您自己的上下文管理器:他的回答。

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

解决方案 2:

以下是用于更改工作目录的上下文管理器示例。它比其他地方提到的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)

或者尝试更简洁的等效方法(如下),使用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.

解决方案 3:

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'.

解决方案 4:

我会os.chdir这样使用:

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

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

更多详情

解决方案 5:

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

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 时,该目录将丢失。如果迫切需要,您可以使用以下可怕的黑客方法更改 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)+"
")

解决方案 7:

os.chdir()才是正确的做法。

解决方案 8:

os.chdir()是 的 Pythonic 版本cd

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

解决方案 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:

(PyPI 上提供的第三方包,不同于)Path中的对象为此目的提供了上下文管理器和方法:path`pathlib`chdir

from path import Path  # pip install path

with Path("somewhere"):
    ...

Path("somewhere").chdir()

解决方案 13:

Python 3.11 现在提供contextlib.chdir:

import contextlib

with contextlib.chdir(path):
    ...

解决方案 14:

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

pwd

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

解决方案 15:

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

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] ) + '
'

# 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)
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用