Python Forum
python keeps opening script directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python keeps opening script directory (/thread-34393.html)



python keeps opening script directory - Tyrel - Jul-27-2021

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?


RE: python keeps opening script directory - snippsat - Jul-27-2021

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)



RE: python keeps opening script directory - Tyrel - Jul-27-2021

(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


RE: python keeps opening script directory - j.crater - Aug-06-2021

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?


RE: python keeps opening script directory - Axel_Erfurt - Aug-06-2021

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()



RE: python keeps opening script directory - snippsat - Aug-06-2021

(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?