Update MetadataManager.py

Also, restore cwd when exceptions are thrown.