How do I write to a website/HTML Documet using python 3? -


i'm using python 3 , able read code html document unable write it. how go this. i'll show mean:

 import urllib.request   locator=urllib.request.urlopen("file:///e:/programming/calculator.html", "r")  transfer=locator.read()  print("\n\n",transfer, "\n")  locator.close()   locator=urllib.request.urlopen("file:///e:/programming/calculator.html","w+")  locator.write("<p> hello site has been changed</p>")  locator.close()   locator=urllib.request.urlopen("file:///e:/programming/calculator.html","r")  new=locator.read()  print(new)  locator.close() 

so i'm read can't write or change of it's code. why this?

also, tried read actual url website using exact same code above replacing url , removing write function. interpreter came error, , wasn't able read site. how can read website too?

note: i'm learning, i'm not gonna illegal want become more knowledgeable kind of stuff

also if change write append() still produces error

                         import urllib.request                            locator=urllib.request.urlopen("file:///e:/programming/calculator.html", "r")                           transfer=locator.read()                           print("\n\n",transfer, "\n")                          locator.close()                            locator=urllib.request.urlopen("file:///e:/programming/calculator.html", "w+")                    open("file:///e:/programming/calculator.html") f:                    f.write('something')                     locator.close() 

the above piece of code suggested member ut instead of writing url error saying:

                traceback (most recent call last):                 file "c:\users\kenny\desktop\python\practice.py", line 10, in <module>                 open("file:///e:/programming/calculator.html") f: 

oserror: [errno 22] invalid argument: 'file:///e:/programming/calculator.html'

ignore spacing way pasted it. code should in line open part f.write function idented

urllib.request.urlopen returns file-like object.

these objects expose read method, should not allow write.

i think of as, when using urllib requesting file if typed resource browser. can't write it. request , served you.

if want open file writing can use open method

with open('e:/programming/calculator.html', 'w+') f:    f.write('something') 

the above example uses with statement shortcut manually closing file when code exits block.

it similar

f = open('e:/programming/calculator.html', 'w+') f.write('something') f.close() 

@lattyware posted great tutorial on it, many more can found online. pep outlines for.

it seems might confusing urlopen , open command.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -