Python Forum
python keeps opening script directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python keeps opening script directory
#1
Could someone please help,

openwebsites.py:

import webbrowser
import os


def websites():
    websitelist = "Setup1\\web_value.txt"
    websites = []
    browser = webbrowser.get("windows-default")
    for line in open(websitelist, "r").readlines():
        websites.append(line.strip())
    for website in websites:
        browser.open(website)
websites()
This script opens a list of websites from a file that holds the urls. it works perfectly but it keeps opening the directory of openwebsites.py. What am I doing wrong?
Reply
#2
Do you mean that it open directory of openwebsites.py in browser?
If i do a test your code it only open sites in .txt file in browser.
I would write it like this so file object get closed,but i can not get your behavior just open sites in browser.
import webbrowser
import os

def websites(websitelist):
    browser = webbrowser.get("windows-default")
    with open(websitelist) as web_lst:
        for website in web_lst:
            browser.open(website.strip())

if __name__ == '__main__':
    websitelist = "sites.txt"
    websites(websitelist)
Reply
#3
(Jul-27-2021, 08:09 PM)snippsat Wrote: Do you mean that it open directory of openwebsites.py in browser?
If i do a test your code it only open sites in .txt file in browser.
I would write it like this so file object get closed,but i can not get your behavior just open sites in browser.
import webbrowser
import os

def websites(websitelist):
    browser = webbrowser.get("windows-default")
    with open(websitelist) as web_lst:
        for website in web_lst:
            browser.open(website.strip())

if __name__ == '__main__':
    websitelist = "sites.txt"
    websites(websitelist)

No it opens the the folder where the script is stored in file explorer and it opens the websites u don’t want it to open file explorer where the script is stored I just want the script to open websites from a list of websites in a txt file
Reply
#4
Did you try to print the website addresses (after using .strip())?
That way you can make sure the addresses are valid urls.
Maybe encoding argument should be added to open?
Reply
#5
make it shorter, and i think webbrowser always uses the default browser

import webbrowser
import os

websitelist = "Setup1\\web_value.txt"
 
def websites():
    websites = open(websitelist).read().splitlines()
    for website in websites:
        webbrowser.open(website.strip())
        
websites()
Reply
#6
(Aug-06-2021, 09:38 PM)Axel_Erfurt Wrote: make it shorter, and i think webbrowser always uses the default browser
Not necessary to read and split(make a list) and it do not close file object.
If look at my code so dos it just iterate over file object,no read or split used.

But code may not be the problem here this is his other Thread,
so his problem is that file explore (Windows) opens when run code.
Which have never happened to me,so that's' why ask in other Thread how he run the code?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script with subprocess in another directory paul18fr 1 162 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Why is IDLE not opening on a MacOS Montery 12.6 and using Python 3.10.7? Merlin385 7 472 Oct-08-2022, 08:36 PM
Last Post: Merlin385
  How to import another Python in different directory? dee 3 318 Sep-28-2022, 06:41 PM
Last Post: dee
  Opening CMD from Python Oshadha 2 622 Jul-17-2022, 11:22 PM
Last Post: Skaperen
Question Opening small size browser with python selenium not work, need help greenpine 0 865 Feb-07-2022, 11:36 AM
Last Post: greenpine
  Python OpenCV window not opening in fullscreen mode Zman350x 0 2,291 Apr-29-2021, 07:54 PM
Last Post: Zman350x
  problem with sphinx and file directory in script kiyoshi7 0 1,703 Mar-11-2021, 03:52 PM
Last Post: kiyoshi7
  How to kill a bash script running as root from a python script? jc_lafleur 4 3,998 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Make the script read from any directory falahfakhri 2 1,577 Jun-15-2020, 02:18 PM
Last Post: falahfakhri
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 1,632 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020