cm_demo.py 330 B

1234567891011121314151617181920
  1. import os
  2. from contextlib import contextmanager
  3. @contextmanager
  4. def change_dir(destination):
  5. try:
  6. cwd = os.getcwd()
  7. os.chdir(destination)
  8. yield
  9. finally:
  10. os.chdir(cwd)
  11. with change_dir('Sample-Dir-One'):
  12. print(os.listdir())
  13. with change_dir('Sample-Dir-Two'):
  14. print(os.listdir())