简体   繁体   中英

execute python file in batch mode by specifying the list of commands

I got a python file which is a code that I developed. During his execution I input from the keyboard several characters at different stages of the program itself. Also, during the execution, I need to close a notepad session which comes out when I execute into my program the command subprocess.call(["notepad",filename]) . Having said that I would like to run this code several times with inputs which change according to the case and I was wondering if there is an automatic manner to do that. Assuming that my code is called 'mainfile.py' I tried the following command combinations:

import sys
sys.argv=['arg1']
execfile('mainfile.py')

and

import sys
import subprocess
subprocess.call([sys.executable,'mainfile.py','test'])

But it does not seem to work at least for the first argument. Also, as the second argument should be to close a notepad session, do you know how to pass this command?

Maybe have a look at this https://stackoverflow.com/a/20052978/4244387

It's not clear what you are trying to do though, I mean the result you want to accomplish seems to be just opening notepad for the sake of saving a file.

The subprocess.call() you have is the proper way to execute your script and pass it arguments.

As far as launching notepad goes, you could do something like this:

notepad = subprocess.Popen(['notepad', filename])
# do other stuff ...
notepad.terminate()  # terminate running session

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM