Skip to content

Instantly share code, notes, and snippets.

@yuchdev
Created June 10, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuchdev/b05dc762bc146c52befa9e3e53a1e35c to your computer and use it in GitHub Desktop.
Save yuchdev/b05dc762bc146c52befa9e3e53a1e35c to your computer and use it in GitHub Desktop.
Command os.system with ability to enter and leave directory using 'with' statement
class Shell:
def __init__(self, cd_path):
self.cd_path = cd_path
self.exit_path = os.getcwd()
def __enter__(self):
os.chdir(self.cd_path)
def __exit__(self):
os.chdir(self.exit_path)
@staticmethod
def system(command):
"""
Execute command and check return code
"""
logger.info(command)
ret_code = os.system(command)
if ret_code != 0:
logger.warning("{} return code is {}".format(command, ret_code))
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment