<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
<div><br>
<meta http-equiv="Content-Type" content="text/html; charset=unicode">
<meta name="Generator" content="Microsoft SafeHTML">
<style>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Tahoma;}
</style>
<div dir="ltr">
Hi all</div><div dir="ltr"><br></div><div dir="ltr"> I am new to programming and on a very steep curve. I am using Python 3 to learn and I am having trouble understanding exactly what pickling is. <div>I have written a program that asks for a URL and then writes the source code from the URL to a file, then pickles the file I just created. </div><div>I am not sure why I pickle something as opposed to just saving it as a normal file?</div><div><br></div><div>I saved the url source code file as a .txt and also the pickled file as .txt. I am not sure of the filetype I should save a pickled file as.</div><div>When I look at the files (the saved source file and the pickled file) they look the same. I am viewing them in notepad on the Mac.</div><div><br></div><div>Many thanks</div><div>Joe</div><div><br></div><div><div>###################################################################</div><div># Pickle an object- Puzzle 5 </div><div># Joe Batt 18 Nov 2011 - http://www.pythonchallenge.com/pc/def/peak.html</div><div>###################################################################</div><div><br></div><div>import pickle</div><div>import urllib</div><div><br></div><div>def geturlfile(urlfile):</div><div>#gets the user URL from main opens it and saves it to var fileurl</div><div> from urllib.request import urlopen</div><div> fileurl=urlopen(urlfile)</div><div>#opens the file on computer and writes the var fileurl to it</div><div> html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','w')</div><div> for line in fileurl.readlines():</div><div> linestring=line.decode("utf-8") #ensures written in string not byte</div><div> html_file.write(linestring)</div><div> html_file.close() #closes the puzzle file</div><div>#just prints file to check its correct</div><div> html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','r')</div><div> filecontents=html_file.read()</div><div> html_file.close()</div><div> print (filecontents)</div><div> print (type(filecontents))</div><div><br></div><div>#pickles the file filecontents containing the url file</div><div> pickledfile=open('///Users/joebatt/Desktop/python/pickledpuzzle5.txt','wb')</div><div> pickle.dump(filecontents,pickledfile)</div><div> pickledfile.close() </div><div> return ()</div><div><br></div><div> </div><div><br></div><div># Main program</div><div>urlfile=input('Please input the URL ')</div><div>geturlfile(urlfile)</div></div>                                            </div></div>                                            </div></body>
</html>