2

これが私が基本的にやろうとしていることです:

import sh, os

with sh.cd('/tmp'):
  print os.getcwd()

print os.getcwd()

ただし、次のエラーが表示されます

line 3, in <module>
    with sh.cd('/tmp'):
AttributeError: __exit__

ここで何が欠けていますか?コンテキスト内でディレクトリを変更する代替ソリューションはありますか?

4

2 に答える 2

2

sh現在pushd()のディレクトリを一時的に変更するためのコンテキストマネージャとして使用できる機能が追加されました:

import sh

with sh.pushd("/tmp"):
    sh.touch("a_file")

https://amoffat.github.io/sh/sections/command_class.html?highlight=pushd#pushdを参照してください

于 2021-08-29T19:03:41.420 に答える