Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:12:40+00:00 2026-05-26T19:12:40+00:00

I have a Window .bat file with DOS commands that involve REM, MKDIR, ECHO,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:12:41+00:00Added an answer on May 26, 2026 at 7:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Sliksvn Windows client and have a DOS BAT file that contains the
I have a batch file, 'buildAll.bat', that builds a set of projects. It will
I have created a .bat file that displays all the users logged in Windows
I have a bat file that I am using to try and register Mobilink
I have a DOS batch file to run on a daily basis. Something similar
I have a small dos bat file I'm running to rsync a bunch of
I have created a bat file which calls selenium server called run-selenium-server.bat which has:
I have the following problem. I got a bat file that runs testcomplete test.
I have a Hudson job that launches a .bat script file that itself launches
I have a window that is set with NSBorderlessWindowMask, and also kCGDesktopWindowLevel. When a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.