Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslate Window BAT (batch) file into Python script
    text
    copied!<p>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. </p> <p>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. </p> <p>An excerpt example:</p> <pre><code>SET LOG=test.log DEL /F /Q ..\output\ REM THE LOG STARTS HERE ECHO test.bat started &gt;%LOG% ECHO %DATE% &gt;&gt;%LOG% ECHO %TIME% &gt;&gt;%LOG% stata/e statascript.do matlab -r matlabscript -logfile matlabscript.log -nosplash -minimize -wait lyx -e pdf2 document &gt;&gt;%LOG% 2&gt;&amp;1 COPY %LOG%+statascript.log+matlabscript.log %LOG% ECHO test.bat completed &gt;&gt;%LOG% ECHO %DATE% &gt;&gt;%LOG% ECHO %TIME% &gt;&gt;%LOG% PAUSE </code></pre> <hr> <p>My sample code:</p> <pre><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 &lt;Enter&gt; to exit.") </code></pre> <p>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:</p> <p>"The process cannot access the file because it is being used by another process"</p> <p>Any idea?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload