标签归档:cd

Walle-web-Walle-瓦力DevOps开源项目代码部署平台

Walle 2.0-官方主页


字节跳动内推书名:Tob Lark招聘大数据研发、数据分析师,机会极佳,请勿错过.请各位朋友扩散下有需要的同学直达内推链接、帮助内推,君子成人之美,谢谢.

功能强大,且免费开源的walle-web 瓦力终于更新2.0.0了!

Walle让用户代码发布终于可以不只能选择Jenkins!支持各种Web代码发布,php、java、python、go等代码的发布、回滚可以通过Web来一键完成。Walle一个可自由配置项目,更人性化,高颜值,支持Git、多用户、多语言、多项目、多环境同时部署的开源上线部署系统.

2.0.0占用了我几乎所有业余时间,精力与金钱付出换各位使用收益,望各位喜欢不吝顺手star以示支持,项目更好亦反馈予你.目前2.0.0已经发布,请保持关注,我会在公众号更新(在最下面)。

有推广资源(开源文章推荐、大会分享)的同学,请微信联系我,强烈需要帮助.另外,老版本已迁移到walle 1.x的同学务必不要再更新了,两个版本不兼容

功能

  • gitlabRESTful API,类gitlab的权限模型.将来打通gitlab,良心的惊喜
  • 空间管理.意味着有独立的空间资源:环境管理、用户组、项目、服务器等
  • 灰度发布.呼声不断,终于来了
  • 项目管理.Deploy、Release的前置及后置Hook,自定义全局变量;自带检测、复制功能,都贴心到这种程度了
  • websocket实时展示部署中的shell console、跟真的终端长得一样.
  • 完善的通知机制.邮件、钉钉
  • 全新的UI、我自己都被震撼到了,如丝般流畅

架构


预览





安装

快速安装|安装错误|常见错误排解

路线图

  • 预览版2018/12/02
    • 安装文档、前后端代码、数据迁移
  • Alpha2018-12-09
    • 使用文档、故障排除、公众号更新
  • 测试版2018/12/23🎅圣诞夜前夕
    • 钉钉/邮件消息通知
    • 接受官网徽标企业的Trouble Shooting
  • 2.0.02018/12/301个️⃣元旦前夕
    • 项目检测、复制
    • 任务的回滚
    • released tag、使用文档
    • Docker镜像
    • JAVA配置模板
    • php配置模板
    • github5000star
  • 2.0.12019-01-13
    • 项目配置添加自定义变量
    • Python3.7+兼容
  • 2.1.02019-03-22
    • 超管权限完善
    • Dashboard1.0(全新的玩法,欢迎提问题)
    • 3月24日开源中国苏州源创会-开源综合技术主题“开源构建多空间可视化一键部署DevOps平台”
  • 2.2.02019-04-22
    • 网络挂钩(GitLab)
    • 上线时间记录、命令与结果拆分、实时控制台
    • 宿主机资源监控
  • 2.3.02019-05-27
    • 插件化:Maven、npm
    • 管道式
  • 2.4.02019-06-17
    • I18N国际化
  • 2.5.02019/07/29
    • 上线单差异
    • 消息通知定制化:钉钉、邮件、企业微信
  • 2.6.02019-08-19
    • 批量管理服务器
    • 跨空间复制项目
    • 应用程序打包平台
    • Dashboard2.0

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

问题:如何在Python中更改工作目录?

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

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

cd is the shell command to change the working directory.

How do I change the current working directory in Python?


回答 0

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

import os

os.chdir(path)

使用此方法时,有两个最佳实践:

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

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

You can change the working directory with:

import os

os.chdir(path)

There are two best practices to follow when using this method:

  1. Catch the exception (WindowsError, OSError) on invalid path. If the exception is thrown, do not perform any recursive operations, especially destructive ones. They will operate on the old path and not the new one.
  2. Return to your old directory when you’re done. This can be done in an exception-safe manner by wrapping your chdir call in a context manager, like Brian M. Hunt did in his answer.

Changing the current working directory in a subprocess does not change the current working directory in the parent process. This is true of the Python interpreter as well. You cannot use os.chdir() to change the CWD of the calling process.


回答 1

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

Here’s an example of a context manager to change the working directory. It is simpler than an ActiveState version referred to elsewhere, but this gets the job done.

Context Manager: 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)

Or try the more concise equivalent(below), using ContextManager.

Example

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.

回答 2

我会这样使用os.chdir

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

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

这里更多

I would use os.chdir like this:

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

By the way, if you need to figure out your current path, use os.getcwd().

More here


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

cd() is easy to write using a generator and a decorator.

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)

Then, the directory is reverted even after an exception is thrown:

os.chdir('/home')

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

回答 4

如果您使用的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)

If you’re using a relatively new version of Python, you can also use a context manager, such as 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

UPDATE

If you prefer to roll your own:

import os
from contextlib import contextmanager

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

回答 5

正如其他人已经指出的那样,以上所有解决方案仅会更改当前进程的工作目录。当您返回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)+"\n")

As already pointed out by others, all the solutions above only change the working directory of the current process. This is lost when you exit back to the Unix shell. If desperate you can change the parent shell directory on Unix with this horrible hack:

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")

回答 6

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

os.chdir() is the right way.


回答 7

os.chdir()是的Python版本cd

os.chdir() is the Pythonic version of cd.


回答 8

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

import os

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

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

You can use both with os.chdir(abs_path) or os.chdir(rel_path), there’s no need to call os.getcwd() to use a relative path.


回答 9

进一步进入方向指出了由Brian和基于SH(1.0.8+)

from sh import cd, ls

cd('/tmp')
print ls()

Further into direction pointed out by Brian and based on sh (1.0.8+)

from sh import cd, ls

cd('/tmp')
print ls()

回答 10

如果您想执行“ cd ..”选项,只需键入:

os.chdir(“ ..”)

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

If You would like to perform something like “cd..” option, just type:

os.chdir(“..”)

it is the same as in Windows cmd: cd.. Of course import os is neccessary (e.g type it as 1st line of your code)


回答 11

如果您使用spyder和love GUI,则只需单击屏幕右上角的文件夹按钮,然后浏览要用作当前目录的文件夹/目录。完成此操作后,您可以转到spyder IDE中窗口的文件浏览器选项卡,然后可以看到其中存在的所有文件/文件夹。要检查当前工作目录,请转到spyder IDE的控制台,然后键入

pwd

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

If you use spyder and love GUI, you can simply click on the folder button on the upper right corner of your screen and navigate through folders/directories you want as current directory. After doing so you can go to the file explorer tab of the window in spyder IDE and you can see all the files/folders present there. to check your current working directory go to the console of spyder IDE and simply type

pwd

it will print the the same path as you have selected before.


回答 12

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

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)

Changing the current directory of the script process is trivial. I think the question is actually how to change the current directory of the command window from which a python script is invoked, which is very difficult. A Bat script in Windows or a Bash script in a Bash shell can do this with an ordinary cd command because the shell itself is the interpreter. In both Windows and Linux Python is a program and no program can directly change its parent’s environment. However the combination of a simple shell script with a Python script doing most of the hard stuff can achieve the desired result. For example, to make an extended cd command with traversal history for backward/forward/select revisit, I wrote a relatively complex Python script invoked by a simple bat script. The traversal list is stored in a file, with the target directory on the first line. When the python script returns, the bat script reads the first line of the file and makes it the argument to cd. The complete bat script (minus comments for brevity) is:

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

The python script, dSup.py is:

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)