Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate Window BAT (batch) file into Python script

I have a Window .bat file with DOS commands that involve REM, MKDIR, ECHO, run some programs, and especially combining all the output into a log file with time stamps. I want to translate this into Python.

I tried os.system but this will not work - it will run the programs I need to run, but there is no way to connect all the statements, making sure all the logs are outputted to the right file. As I understand, each time the command line is called separately using os.system.

An excerpt example:

SET LOG=test.log

DEL /F /Q ..\output\

REM THE LOG STARTS HERE
ECHO test.bat started   >%LOG%
ECHO %DATE%     >>%LOG%
ECHO %TIME%     >>%LOG%

stata/e statascript.do  
matlab -r matlabscript -logfile matlabscript.log -nosplash -minimize -wait
lyx -e pdf2 document >>%LOG% 2>&1
COPY %LOG%+statascript.log+matlabscript.log %LOG%


ECHO test.bat completed >>%LOG%
ECHO %DATE%     >>%LOG%
ECHO %TIME%     >>%LOG%

PAUSE

My sample code:

import subprocess, time, sys, os

log = open('test.log', 'w')

p = subprocess.call(r'DEL /F /Q ..\output\\', shell = True)

log.write('test.py started\n')
log.write(time.asctime())
log.write('\n\n\n')
log.flush()

p = subprocess.call(r'%STATAEXE% /e do statacode.do', shell = True, stdout = log)
log.write(open('statacode.log').read())
p = subprocess.Popen('DEL statacode.log', shell = True, stdout = log)


log.write('\n\ntest.py completed\n')
log.write(time.asctime())
log.write('\n')
log.flush()
log.close()
raw_input("Press <Enter> to exit.")

The only issue I have left is: when I run this in Windows by double clicking on test.py the way I would do it with test.bat, I get this error warning message even though all the steps are completed in the end:

"The process cannot access the file because it is being used by another process"

Any idea?

like image 478
cinny Avatar asked Aug 31 '26 14:08

cinny


1 Answers

You should probably be using the subprocess module to invoke your executables. It allows you to specify things like the input and output handles for the process, so you can direct them to where you want.

like image 181
Amber Avatar answered Sep 03 '26 04:09

Amber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!