Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening and Closing Programs with Command Line

Status
Not open for further replies.

t35li

Programmer
May 28, 2009
12
CA
I'm writing a Python program that bascially opens a program, does something to it, and close the program, all through command line. So far I have program with closing the program. For an example :

import os

os.system("notepad")
..
(does something with notepad and saves the file somewhere)
..
os.system("taskkill /im notepad.exe /f")

The notepad opens, but it doesnt close. And when i close it manually by clicking the X button, it says:

Error: The process "notepad.exe" not found.

any suggestions?
 
just to make it clear, I'm not trying to read or write to a text file, even though i used notepad as an example.
 
It is because os.system(command) runs command and waits until the command ends.
It means, that while your notepad runs, your python program never comes to the execution of the next os.system(command), which should kill the previous command.
Then at end when you kill your notepad manually, python executes os.system("taskkill /im notepad.exe /f"), but it doesn't found the notepad process, because you terminated it, and so you get the error 'Error: The process "notepad.exe" not found'

You can easily solve this problem starting the notepad process in the separate instance using the windows start command

Example:

notepad_start_and_stop.py
Code:
[COLOR=#a020f0]import[/color] os

os.system("[COLOR=#ff00ff]start notepad.exe[/color]")
kill=raw_input("[COLOR=#ff00ff]Do you want to kill all running notepad ? (Y/y)[/color]")
kill = kill.lower()
[COLOR=#804040][b]if[/b][/color] kill == '[COLOR=#ff00ff]y[/color]':
  [COLOR=#804040][b]print[/b][/color] "[COLOR=#ff00ff]Now I'm killing all running notepads[/color]"
  result=os.system("[COLOR=#ff00ff]taskkill /im notepad.exe /f[/color]")
  [COLOR=#0000ff]# process result[/color]
  [COLOR=#804040][b]if[/b][/color] result == 0:
    [COLOR=#804040][b]print[/b][/color] "[COLOR=#ff00ff]All nodepads should be death now...[/color]"
  [COLOR=#804040][b]else[/b][/color]:
    [COLOR=#804040][b]print[/b][/color] "[COLOR=#ff00ff]Error executing taskkill command !!![/color]"
[COLOR=#804040][b]else[/b][/color]:
  [COLOR=#804040][b]print[/b][/color] "[COLOR=#ff00ff]I let them running. Kill them self if you want ![/color]"
Running..
Code:
D:\Work>python notepad_start_and_stop.py
Do you want to kill all running notepad ? (Y/y)[COLOR=red]y[/color]
Now I'm killing all running notepads
SUCCESS: The process "notepad.exe" with PID 1556 has been terminated.
All nodepads should be death now...
 
Thx, this solves a bit of my problem, but want all this process to be in a loop. So it would start the program, load a file with the program, save the output of the program, and close it, and then loop again with different input files to the program. If you know what I mean. So I probably wont want to input whether i want to kill the program or not. Some one else suggested using cmd /c "start notepad.exe" command, i'm not sure how that would work.
 
t35li said:
So it would start the program, load a file with the program, save the output of the program, and close it, and then loop again with different input files to the program.

IMHO you cannot save the output of notepad outside of notepad, or do you mean notepad has a "batch mode"? IMHO it's not possible.
 
If you want edit your text files in batch mode (i.e. non interactive) use rather non interactiv editor like sed instead of notepad,
or you can process text files with every language like: perl, python, rexx, ruby, ... etc
 
hmm...
The program is actually an image creator. It loads a text file, create an image using the informations, and save the image to somewhere. I have over 300 of those text files that I want to create an image for, and I dont want to do each one manually. So I wanted to put all this in a loop and automate this process. I like the sound of the batch processing, any way I can do this??
 
Ok I thought at notepad, but when your image creator program could process an image file in batch mode the it's ok.
Try to call simply os.system(your_image_program_command_line) in a loop. I don't see any problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top