TGViewer
Питонические атаки Питонические атаки @pythonic_attacks · 1.15K subscribers
Post #526 1.01K

Forwarded from Python etc

I often find myself writing a context manager to temporarily change the current working directory:

import os
from contexlib import contextmanager

@contextmanager
def enter_dir(path):
old_path = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_path)


Since Python 3.11, a context manager with the same behavior is available as contextlib.chdir:

import os
from contextlib import chdir

print('before:', os.getcwd())
# before: /home/gram
with chdir('/'):
print('inside:', os.getcwd())
# inside: /
print('after:', os.getcwd())
# after: /home/gram
  • 👍 20
  • 😱 3
  • ❤ 2
  • 🎉 1
  • 🖕 1
More from @pythonic_attacks
  1. May 24, 2026GPT-5.5 по дефолту предпочитает использовать uv. Совпадение? Абсолютно точно нет. Логично…
  2. Mar 19, 2026Новость вызывает столько противоречивых мыслей, что я даже прерву радио молчание. С одной…
  3. Mar 19, 2026https://openai.com/index/openai-to-acquire-astral/ & https://astral.sh/blog/openai Это не…
  4. Nov 17, 2025Ну что же.. Мы совместно с Эммой Смит опубликовали Pre-PEP о добавлении Раста в CPython. В…
  5. May 19, 2025Вышел трейлер документалки о Python. Что-то на уровне Marvel, возможно даже выше https://w…
  6. May 15, 2025All good things come to an end Что ж, скажем Майкрософту спасибо, что 4 года содержал кома…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →