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.

Community
  • 1
  • 1
AlienAnarchist
  • 186
  • 4
  • 15
  • The error message is always worth a look, could you copy-paste it inside your question? – Joël Mar 13 '14 at 17:19

1 Answers1

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.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • 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 Mar 13 '14 at 19:48
  • @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. – anon582847382 Mar 13 '14 at 19:57