-2

I am trying to make a program which opens an URL written in a text file. My program reads from the file and tries to open the url but gets the error

TypeError: startfile: filepath should be string, bytes or os.PathLike, not list

I have tried readlines(), readline() as i wish to open the second url in the file.

Here is the code

import webbrowser as wb

r_file = open("Websites.txt","r")


url = r_file.readlines()



print(url)


wb.open(url, new=0)

r_file.close()
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59

3 Answers3

0

It's simplest to process lines in a file using a with statement (files are context managers in Python, meaning they are designed for use in with statements and the like):

open("Websites.txt","r") as in_file:
    for url in_file:
        wb.open(url.strip(), new=0)
        input("Next page: ")

I've taken the precaution of removing any whitespace from the start or end of the URL, and added an input call to give you chance to bring the URLs up one by one.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

Try the following code

import webbrowser as wb

r_file = open("C:\Users\RichardSkeggs\OneDrive - Alchemmy\prj\Belron\data\Websites.txt","r")

url = r_file.readlines()

for line in url: print(line) line=line.strip('\n') wb.open(line, new=0)

r_file.close()

The first step is loop through the list of entries picked up from the text file. You need to remove the newline character.

rickskeggs
  • 24
  • 1
0

.readlines returns a list but wb.open() waits a string you can use something like that to solve your problem

import webbrowser as wb

r_file = open("Websites.txt","r")

urls = r_file.readlines()
for url in urls:
    print(url)
    wb.open(url, new=0)
r_file.close()