0

I am working on a python script that will walk through a large collection of .py files and write out individual .bat files that can be called upon to run these scripts.

I understand typical python output

directory = 'c:/'
OPATH = open(str(directory) + 'output_file.txt', 'w')

However if I try to do output_file.bat I receive an error, and it won't let me write out to it.

What I would like written in the batch files. Creating a BAT file for python script

Is there any documentation on how to write out other kinds of files with python? I would also be interested in having a python script generate .c files as well.

1
  • The error message is always worth a look, could you copy-paste it inside your question? Commented Mar 13, 2014 at 17:19

1 Answer 1

1

Try this, using os.path.join(). Your error was merely trivial, don't worry.

import os

directory = 'C:/'
with open(os.path.join(directory, 'output_file.bat'), 'w') as OPATH:
    OPATH.writelines(['@echo off', 
                      'c:\python27\python.exe c:\somescript.py %*', 
                      'pause'])

This provides a cross-platform solution to your problem. Although your error was due to a missing /, you should not hardcode this. This is the best way to join two paths and thus solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm an idiot, sorry its been a long day in the office. Only other difference is the output is going to be 'output_file.bat'
@AlienAnarchist In which case you can just change the .txt to a '.bat' in the code as shown in my edits. Python can edit this file type just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.