0

The coding that I am using to learn:

import requests


r = requests.get("http://google.com")

f = open("./page.html", "w+")
f.write(r.text)

So if i understand all right this will create a .html file and write the google.com html code that i request (r) in it.

If i run this now:

import requests

params = {"q": "pizza"}
r = requests.get("http://google.com/search", params=params)

f = open("./page.html", "w+")
f.write(r.text)

Does the f.write(r.text) rewrite everything that's on 'r' variable like it was a blank file or it will just edit the file with new stuff i add into 'r'? didn't knew how to ask. Thanks

EDIT: nevermind I am a noob! use f = open("./page.html", "a") if i want to append and 'w' if i want to overwrite

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
João Fróis
  • 89
  • 10

1 Answers1

1

That all depends on how you open the file

The mode indicates, how the file is going to be opened "r" for reading, "w" for writing and "a" for a appending.

f = open("./page.html", "r")
f = open("./page.html", "w")
f = open("./page.html", "a")
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65