I'm using python to run a script from a .bat file that opens a piece of software for data analysis. After the script is finished, the cmd is still open. How do I close it after the script terminates? Currently using:
subprocess.Popen('name.bat')
Related
I am trying to write a batch script which, once complete, would allow the user to continue using the Windows command prompt as they normally would had no script been run. Is this possible? Thank you in advance for any help.
If you manually open CMD (the Command Prompt) and invoke the batch file by name, CMD will remain open for additional commands after the batch file completes. You cannot do this by double-clicking on the batch file, but if you create a shortcut to the batch file that runs CMD.EXE with the /K switch, you will run the batch file and then leave CMD running for additional commands. See CMD at SS64.
I'm trying to schedule in task scheduler:
Run a batch file
Activate a conda environment
Run a python program
Exit command prompt
Everything works fine, except the python.exe window will remain open while the command prompt closes.
My batch file: (the sleep is for the python code to run. It takes a few seconds)
call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit
My python script:
driver = webdriver.Chrome(executable_path="C:\path\to\chromedriver")
driver.get('http://website.com')
# Find email and pw fields and then fill them in
email = driver.find_element_by_id("user_email")
email.send_keys('myemail#email.com')
pw = driver.find_element_by_id("user_password")
pw.send_keys('password')
# Click on sign-in button
driver.find_element_by_class_name("button").click()
time.sleep(5)
# Click on save button to update
driver.find_element_by_class_name("button").click()
# Close driver
driver.close()
Last thing, the program/script is the batch file, no arguments, and the start in is in the directory that the batch file is in.
Any help would be appreciated!
put you python codes in a main() function.
and give:
if __name__ == '__main__':
main()
at the end.
Just tested works for me.
#pk2019 's answer really helped me.
One improvement is to use
drv = webdriver.Chrome()
# Do your things.
...
drv.close()
drv.quit()
No need to do the dirty work of killing task.
I'm not a python expert, but I think you just need to call sys.exit() or quit().
Instead of using sleep and waiting too long or possibly not long enough, call start with the wait option:
call activate python2
start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
exit
If you don't need the batch file to do anything else, you can just start the python script and exit.
I had a similar problem with a Python selenium web scraper running geckodriver.exe Firefox web driver on Windows (executed via Task Scheduler using a .bat file). The problem is that my geckodriver.exe process is still running after the Python script is done ... and I think that running process is preventing the Windows command prompt from closing.
In order to test this hunch, I inserted the tasklist command into the .bat file, both before and after the Python script. It prints out a list of running Windows tasks to the console ... I noticed the geckodriver.exe file was still running the Python script was finished.
The way to kill a Windows process (using taskkill) is described in these two different Stackoverflow responses:
Safely killing a python script on Windows
Batch script to close all open Command Prompt windows
How to close Command Prompt window after Batch file execution in python?
Here is the Windows documentation for taskkill:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
In my case, I added this as the 2nd to last line: taskkill /im geckodriver.exe /f, and the last line was exit. That worked.
I am trying to open a xlsx file in a Perl script with the command :
system("path_to_file\\file.xlsx");
The file is opened but it keeps the perl process hanged until I close the .xlsx file. How can I avoid that?
The file file.xlsx is created in the perl script. At the end of the execution I want it to be displayed to the user (like you would manually open it) and it works but it keeps the perl process hanging until i close it.
On Windows, system(1, $cmd) will run the command and immediately return without waiting for the child to finish. See perlport.
On Unix, fork and exec will work to this end.
I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.
I'm running a script from the cmd prompt. This script opens another cmd prompt and runs another batch file there. I want to wait for the error code and then send it back to the original cmd window. Is there a nice way to do this without writing the error code to a file?
Thanks,
Li
If I inderstand you correctly, you want this solution. It solves the problem of returning error level to the calling script from the script that was run in a separate cmd session.