0

I have the following path of HTML page saved in local directory: "C:\Users\Sony\Desktop\b.html". Now I want to search for a word in this page:

url = r"C:\Users\Sony\Desktop\b.html"
page = open(url)

I want to write it as:

k="C:\Users\Sony\Desktop\b.html"
url = r k
page = open(url)

It is giving me error, how to apply read mode r in this case because if I write url=r"k" the k won't be taken as a variable but a string.

I am getting this error:

IOError: [Errno 22] invalid mode ('r') or filename: 'C:\Users\Sony\Desktop\x08.html'

1 Answer 1

0

The raw string prefix only applies to literals. The string is already irreparably damaged, since it is impossible to unilaterally determine without inspection whether the original was supposed to be r'\b' or r'\x08'. All you can do is perform a replace operation and hope you pick the right one.

k = k.replace('\x08', r'\x08')
Sign up to request clarification or add additional context in comments.

2 Comments

text_files = [f for f in os.listdir(path) if f.endswith('.html')] print text_files , this gives me a list of html files ['a.html', 'b.html', 'c.html', 'd.html', 'e.html', 'f.html', 'g.html'] now I want to open each of these files stored locally and find whether a word is present there or not ? I have done this for a paricular file when I give it's whole path , now the problem is if I have stored them in list then how to iterate ?
url="C:\Users\Sony\Desktop\b.html" url = url.replace(url,r') how to replace "C:\Users\Sony\Desktop\b.html" with r"C:\Users\Sony\Desktop\b.html" when I have stored "C:\Users\Sony\Desktop\b.html" in variable url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.