Created
June 10, 2021 17:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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