Utility Python

Python and the System
Environment:

import os print(os.environ["computername"])

Get output from command:

import subprocess def run(cmd): try: output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, universal_newlines=True) except subprocess.CalledProcessError as e:        output = e.output pass return output print(run("dir /p"))

Check existence of dir:

if os.path.isdir("some_dir"): print("some_dir exists")

Filter File
def filter_file(infilename, outfilename, search_regex, replace): f = open(infilename, 'r') filtered_lines = [re.sub(search_regex, replace, line) for line in f.readlines] f.close f = open(outfilename, 'w') f.write("".join(filtered_lines)) f.close