Webbrowser() reading through a text file for URLS
<p>I am trying to write a script to automate browsing to my most commonly visited websites. I have put the websites into a list and am trying to open it using the <code>webbrowser()</code> module in Python. My code looks like the following at the moment:</p>

<pre><code>import webbrowser                                                                                
f = open("URLs", "r")
list = f.readline()
for line in list:
    webbrowser.open_new_tab(list)
</code></pre>

<p>This only reads the first line from my file "URLs" and opens it in the browser. Could any one please help me understand how I can achieve reading through the entire file and also opening the URLs in different tabs?</p>

<p>Also other options that can help me achieve the same.</p>

<p>You're not reading the file properly. You're only reading the first line. Also, assuming you were reading the file properly, you're still trying to open <code>list</code>, which is incorrect. You should be trying to open <code>line</code>.</p>

<p>This should work for you:</p>

<pre><code>import webbrowser

with open('file name goes here') as f:
    all_urls = f.read().split('\n')
    for each_url in all_urls:
        webbrowser.open_new_tab(each_url)
</code></pre>

<p>My answer is assuming that you have the URLs 1 per line in the text file. If they are separated by spaces, simply change the line to <code>all_urls = f.read().split(' ')</code>. If they're separated in another way just change the line to split accordingly.</p>

python python-2.7 python-webbrowser