1

I get the syntax error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

when running the below code.

It is a little hard to find a good solution for this problem on windows which I am running as compared to UNIX which I can find working code for.

from subprocess import Popen, check_call



p1 = Popen('start http://stackoverflow.com/')

p2 = Popen('start http://www.google.com/')

p3 = Popen('start http://www.facebook.com/')



time.sleep(60)

for pid in [p1.pid,p2.pid,p3.pid]:

    check_call(['taskkill', '/F', '/T', '/PID', str(pid)])

I want the code to open the pages for 60 seconds and then close them.

I know there is similar topic on the link:

, but firstly it is for python 2 and I have tried the codes using the subprocess module and they are identical to the code I am using which does not work.

2 Answers2

0

Windows might be thrown off by the slashes in the url. I dont have a windows machine right now but could you try quoting the urls?

It would be nice if you got this working, but the proper python way to do this is to use the webbrowser module. Like this:

import webbrowser 

url = "google.com"
webbrowser.open_new_tab(url)

closing these would be more tricky but i think your question is a duplicate of this, with almost exactly the same code and also for windows. So i think you should search for your answer there first.

How to close an internet tab with cmd/Python?

jonathan
  • 590
  • 3
  • 14
  • I tested it on a windows vm and did some more research. It should work when you quote the url and use the explorer command instead of the start command. Using the explorer command will start your default browser – jonathan Apr 06 '19 at 12:45
  • yeah, the closing is the hard part which I cannot find working code for. – Peter Kanerva Apr 06 '19 at 19:19
  • The link you provided is for python 2. – Peter Kanerva Apr 06 '19 at 19:20
  • oh i'm sorry for that. i didn't notice. However, windows commands have stayed the same from python2 to python3 so that shouldn't matter too much – jonathan Apr 06 '19 at 19:43
0

Replace

p1 = Popen('start http://stackoverflow.com')

with

p1 = Popen('start http://stackoverflow.com',shell=True)

to make sure the command is executed through the shell.

If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Source: https://docs.python.org/3/library/subprocess.html

glhr
  • 4,439
  • 1
  • 15
  • 26
  • Sure, opening the program works but the browser will still not close on the check_call() – Peter Kanerva Apr 06 '19 at 19:17
  • @PeterKanerva you are correct, because the other part of your code is incorrect. I cannot provide a complete solution, because I cannot test my code (I use UNIX), so anything I post would be pseudo code. – Life is complex Apr 06 '19 at 20:44