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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.