import os import os.path import subprocess import traceback def log(s): if s: try: print('%s' % (s)) except: print('%s' % (repr(s))) def execute_cmd_args(args): try: log('Trying to execute: "%s" in dir: "%s"' % (' '.join(args), os.getcwd())) fn = os.path.join(os.getcwd(), args[0]) if os.path.isfile(fn): log('file exists: %s' % (fn)) else: log('file NOT exists: %s' % (fn)) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdoutdata, stderrdata) = proc.communicate() log(stdoutdata) log(stderrdata) except: s = traceback.format_exc() log('Exception while trying to execute %s\n%s' % (args[0], s)) def create_batch(batch_name): f = open(batch_name, 'w') f.write('time /t') f.close() def test(): wrk_dir = 'test' batch_base = 'batch_test.bat' batch_name = wrk_dir + '\\' + batch_base if not os.path.isdir(wrk_dir): os.mkdir(wrk_dir) create_batch(batch_name) os.chdir(wrk_dir) execute_cmd_args([batch_base, ]) test()